import { traduction } from '@/utils/language';
import { IRule } from '@/model/Service/vnc-interface';
import { pwdLevelFn } from '@/validators/get-security-level';
import { USERNAME_LENGTH_AND_SPECIAL_WORD } from '@/utils/regular';
import {
strLengthVerification,
atLeastContainVerification,
containSpecialStrVerification,
securityVerification
} from '@/validators/password-rules';
import { getStoreData } from '@/utils/composition';
import { regVerification } from '@/validators/password-rules';
import { strVerification, setCircleAndLineStyle } from '@/validators/validation-functions';
const ordinaryRule = (
minLength: number,
maxLength: number,
message: string,
required: boolean,
trigger?: string,
containsEmptyString?: boolean
): IRule => {
return {
type: 'password',
required: required,
minLength: minLength,
maxLength: maxLength,
containsEmptyString: containsEmptyString || false,
tipText: traduction('VALIDATOR_PWD_STANDARD', [1, 20]),
validator: strVerification,
message: message,
trigger: trigger || 'blur'
};
};
function notEquireAndBackEquire(editData: any) {
return {
icon: true,
validator: (rule: any, val: string) => {
const str = editData.userName.split('').reverse().join('');
const condition = editData.userName && (editData.userName === val || str === val);
if (!val || condition) {
return false;
}
return true;
},
label: traduction('VALIDATOR_PWD_VALIDATOR4'),
state: false
};
}
export function userNameVerification(rule: any, value: string, callback: any) {
const errMsg = traduction('VALIDTOR_FORMAT_ERROR');
if (!value) {
callback(new Error());
return false;
}
if (rule.pattern) {
if (!new RegExp(rule.pattern).test(value)) {
callback(new Error(errMsg));
return false;
}
} else {
if (
!USERNAME_LENGTH_AND_SPECIAL_WORD.test(value) ||
!/^[^#+-].*$/.test(value) ||
value === '.' ||
value === '..'
) {
callback(new Error(errMsg));
return false;
}
}
callback();
return true;
}
function requiredAndSecurity(
val: string,
callback: any,
verificationList: any,
security: boolean,
errorMessage: string[],
required?: boolean
): boolean {
if (!required && !val) {
callback();
return true;
} else {
let num = 0;
let msg = '';
verificationList.forEach((item: any, index: number) => {
if (item.state) {
num++;
} else if (!msg) {
msg = errorMessage[index === 0 ? 1 : index];
}
});
if (security) {
if (num === verificationList.length - 1) {
callback();
return true;
}
} else {
if (num === verificationList.length) {
callback();
return true;
}
}
callback(new Error(msg));
return false;
}
}
export function passwordVerification(rule: IRule, val: string, callback: any) {
const { errorMessage, required } = rule;
if (required && !val) {
callback(new Error(errorMessage[0]));
return false;
}
let security = false;
const { verificationList } = rule;
if (
verificationList &&
typeof verificationList === 'object' &&
verificationList instanceof Array
) {
verificationList.forEach((item: any) => {
const { validator } = item;
if (validator && typeof validator === 'function' && validator instanceof Function) {
item.state = validator(item, val);
if (item.security) {
security = true;
const level = pwdLevelFn(val, verificationList);
setCircleAndLineStyle(level, item, val);
}
}
});
} else {
callback();
return false;
}
return requiredAndSecurity(val, callback, verificationList, security, errorMessage, required);
}
export const specialRule = (
minLength: number,
maxLength: number,
editData: any,
required: boolean,
errorMessage: string[],
trigger?: string,
containsEmptyString?: boolean
): IRule => {
const localAccountPasswordRulePolicy = getStoreData('glob', 'localAccountPasswordRulePolicy');
const localAccountPasswordPattern = getStoreData('glob', 'localAccountPasswordPattern');
let verificationList: IRule = [];
if (localAccountPasswordRulePolicy === 'Customized') {
verificationList = [
strLengthVerification({
rangeSize: [minLength, maxLength],
label: traduction('VALIDATOR_PWD_VALIDATOR1', [minLength, maxLength])
}),
regVerification(localAccountPasswordPattern, 'local'),
securityVerification()
];
} else if (localAccountPasswordRulePolicy === 'Hybrid') {
verificationList = [
strLengthVerification({
rangeSize: [minLength, maxLength],
label: traduction('VALIDATOR_PWD_VALIDATOR1', [minLength, maxLength])
}),
atLeastContainVerification(),
containSpecialStrVerification(),
notEquireAndBackEquire(editData),
regVerification(localAccountPasswordPattern, 'local'),
securityVerification()
];
} else {
verificationList = [
strLengthVerification({
rangeSize: [minLength, maxLength],
label: traduction('VALIDATOR_PWD_VALIDATOR1', [minLength, maxLength])
}),
atLeastContainVerification(),
containSpecialStrVerification(),
notEquireAndBackEquire(editData),
securityVerification()
];
}
return {
type: 'password',
required: required,
containsEmptyString: containsEmptyString ? containsEmptyString : false,
verificationList: verificationList,
validator: passwordVerification,
errorMessage,
trigger: trigger || 'blur'
};
};
export function computeRepeatTimes(value: string = ''): number {
let str1 = '';
let str2 = '';
const vLength = value.length;
for (let i = 1; i <= vLength; i++) {
if (vLength % i === 0) {
for (let m = i; m < vLength; m += i) {
str1 = value.substring(0, i);
str2 = value.substring(m, m + i);
if (str1 !== str2) {
break;
} else if (m + i === vLength) {
return vLength / i;
}
}
}
}
return 1;
}
export function errorTip(msgId: string): any {
let alertObj = {
type: 'error',
label: 'COMMON_FAILED'
};
if (msgId === 'AuthorizationFailed') {
return null;
}
const successTips = traduction(`${msgId}.successTips`);
const errorMessage = traduction(`${msgId}.errorMessage`);
if (successTips !== `${msgId}.successTips`) {
alertObj = {
type: 'success',
label: successTips
};
} else {
if (msgId === 'InsufficientFreeSpace') {
alertObj = {
type: 'error',
label: traduction('COMMON_FREE_SPACE')
};
} else {
alertObj = {
type: 'error',
label: errorMessage || traduction('COMMON_FAILED')
};
}
}
return alertObj;
}
export function getMessageId(error: any) {
let errorObject = '';
const errorIdArr: any = [];
if (typeof error === 'string') {
try {
errorObject = JSON.parse(error);
} catch (err) {
return errorIdArr;
}
} else {
errorObject = error.data || error;
}
if ((errorObject as any).error && Array.isArray((errorObject as any).error)) {
(errorObject as any).error.forEach((errorItem: any) => {
errorIdArr.push({
errorId: errorItem.code,
description: errorItem.message,
relation: ''
});
});
} else if ((errorObject as any).error && (errorObject as any).error['@Message.ExtendedInfo']) {
(errorObject as any).error['@Message.ExtendedInfo'].forEach((item: any) => {
const oldErrorId = item.MessageId.split('.').reverse().shift();
errorIdArr.push({
errorId: oldErrorId,
description: item.Message,
relation: item.relatedProperties
});
});
} else {
}
return errorIdArr;
}
* 根据是否开启密码检查初始化password类型输入框的校验规则
* passwordCheck:是否启用密码检查(用户&安全-安全配置-密码检查)
*/
export function initPasswordRules(
passwordCheck: boolean,
minPwdLength: number,
editData: any,
required: boolean,
errorMessage: string[],
trigger?: string,
containsEmptyString?: boolean
) {
if (passwordCheck) {
return specialRule(
minPwdLength,
20,
editData,
required,
errorMessage,
trigger,
containsEmptyString
);
} else {
return ordinaryRule(
1,
20,
traduction('VALIDTOR_FORMAT_ERROR'),
required,
trigger,
containsEmptyString
);
}
}