|
| 1 | +package org.zstack.core.convert; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.springframework.beans.factory.annotation.Autowire; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.beans.factory.annotation.Configurable; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | +import org.zstack.core.encrypt.EncryptFacade; |
| 9 | +import org.zstack.core.encrypt.EncryptGlobalConfig; |
| 10 | +import org.zstack.header.core.encrypt.PasswordEncryptType; |
| 11 | +import org.zstack.utils.Utils; |
| 12 | +import org.zstack.utils.logging.CLogger; |
| 13 | + |
| 14 | +import javax.persistence.AttributeConverter; |
| 15 | +import javax.persistence.Converter; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
| 18 | + |
| 19 | +/** |
| 20 | + * @Author: DaoDao |
| 21 | + * @Date: 2023/3/9 |
| 22 | + */ |
| 23 | +@Component |
| 24 | +@Converter |
| 25 | +@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) |
| 26 | +public class SpecialDataConverter implements AttributeConverter<String, String> { |
| 27 | + private static final CLogger logger = Utils.getLogger(SpecialDataConverter.class); |
| 28 | + |
| 29 | + private static EncryptFacade encryptFacade; |
| 30 | + |
| 31 | + |
| 32 | + @Autowired |
| 33 | + public void init(EncryptFacade encryptFacade){ |
| 34 | + SpecialDataConverter.encryptFacade = encryptFacade; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String convertToDatabaseColumn(String attribute) { |
| 39 | + if (PasswordEncryptType.None.toString().equals(EncryptGlobalConfig.ENABLE_PASSWORD_ENCRYPT.value(String.class))) { |
| 40 | + return attribute; |
| 41 | + } |
| 42 | + if (StringUtils.isEmpty(attribute)) { |
| 43 | + return attribute; |
| 44 | + } |
| 45 | + |
| 46 | + if (!isMobileNO(attribute) && !checkEmail(attribute)) { |
| 47 | + return attribute; |
| 48 | + } |
| 49 | + |
| 50 | + return encryptFacade.encrypt(attribute); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String convertToEntityAttribute(String dbData) { |
| 55 | + if (PasswordEncryptType.None.toString().equals(EncryptGlobalConfig.ENABLE_PASSWORD_ENCRYPT.value(String.class))) { |
| 56 | + return dbData; |
| 57 | + } |
| 58 | + |
| 59 | + if (StringUtils.isEmpty(dbData)) { |
| 60 | + return dbData ; |
| 61 | + } |
| 62 | + |
| 63 | + return encryptFacade.decrypt(dbData); |
| 64 | + } |
| 65 | + |
| 66 | + public static boolean isMobileNO(String mobiles) { |
| 67 | + try { |
| 68 | + Pattern p = Pattern |
| 69 | + .compile("[1][3456789][0-9]{9}$"); |
| 70 | + Matcher m = p.matcher(mobiles); |
| 71 | + return m.matches(); |
| 72 | + } catch (Exception e) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static boolean checkEmail(String email) { |
| 78 | + try { |
| 79 | + String EMAIL_PATTERN = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; |
| 80 | + Pattern pattern = Pattern.compile(EMAIL_PATTERN);; |
| 81 | + Matcher matcher = pattern.matcher(email); |
| 82 | + return matcher.matches(); |
| 83 | + } catch (Exception e) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments