“e5566ef8194b714c679611ac1ab1563c2f3f5053”上不存在“...git@gitcode.net:qq_34446485/graphql-java-codegen.git”
ActionChangePassword.java 3.9 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7
package com.x.organization.assemble.personal.jaxrs.password;

import java.util.Date;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

8
import com.google.gson.JsonElement;
R
roo00 已提交
9 10 11
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.config.Config;
12
import com.x.base.core.project.gson.GsonPropertyObject;
R
roo00 已提交
13 14
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
15
import com.x.base.core.project.jaxrs.WrapBoolean;
R
roo00 已提交
16 17 18 19 20 21 22 23 24 25
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.Crypto;
import com.x.organization.assemble.personal.Business;
import com.x.organization.core.entity.Person;

class ActionChangePassword extends ActionBase {

	private static Logger logger = LoggerFactory.getLogger(ActionChangePassword.class);

26
	ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
R
roo00 已提交
27 28
		/* 管理员不可以修改密码 */
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
29 30
			ActionResult<Wo> result = new ActionResult<>();
			Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
R
roo00 已提交
31 32 33 34 35 36 37 38 39
			Business business = new Business(emc);
			if (Config.token().isInitialManager(effectivePerson.getDistinguishedName())) {
				throw new ExceptionEditInitialManagerDeny();
			}
			Person person = business.person().pick(effectivePerson.getDistinguishedName());
			person = emc.find(person.getId(), Person.class);
			if (null == person) {
				throw new ExceptionPersonNotExisted(effectivePerson.getDistinguishedName());
			}
40
			if (StringUtils.isEmpty(wi.getOldPassword())) {
R
roo00 已提交
41 42
				throw new ExceptionOldPasswordEmpty();
			}
43
			if (StringUtils.isEmpty(wi.getNewPassword())) {
R
roo00 已提交
44 45
				throw new ExceptionNewPasswordEmpty();
			}
46
			if (StringUtils.isEmpty(wi.getConfirmPassword())) {
R
roo00 已提交
47 48
				throw new ConfirmPasswordEmptyException();
			}
49
			if (!StringUtils.equals(wi.getNewPassword(), wi.getConfirmPassword())) {
R
roo00 已提交
50 51
				throw new ExceptionTwicePasswordNotMatch();
			}
52
			if (StringUtils.equals(wi.getNewPassword(), wi.getOldPassword())) {
R
roo00 已提交
53 54 55
				throw new ExceptionNewPasswordSameAsOldPassword();
			}
			if (BooleanUtils.isTrue(Config.person().getSuperPermission())
56
					&& StringUtils.equals(Config.token().getPassword(), wi.getOldPassword())) {
R
roo00 已提交
57 58
				logger.info("user{name:" + person.getName() + "} use superPermission.");
			} else {
59
				if (!StringUtils.equals(Crypto.encrypt(wi.getOldPassword(), Config.token().getKey()),
R
roo00 已提交
60 61 62
						person.getPassword())) {
					throw new ExceptionOldPasswordNotMatch();
				}
R
update  
roo00 已提交
63 64
				if (!wi.getNewPassword().matches(Config.person().getPasswordRegex())) {
					throw new ExceptionInvalidPassword(Config.person().getPasswordRegexHint());
R
roo00 已提交
65
				}
R
update  
roo00 已提交
66 67 68 69

//				if (PasswordTools.checkPasswordStrength(wi.getNewPassword()) < 4) {
//					throw new ExceptionInvalidPassword();
//				}
R
roo00 已提交
70 71
			}
			emc.beginTransaction(Person.class);
72
			person.setPassword(Crypto.encrypt(wi.getNewPassword(), Config.token().getKey()));
R
roo00 已提交
73 74
			person.setChangePasswordTime(new Date());
			emc.commit();
75 76 77
			Wo wo = new Wo();
			wo.setValue(true);
			result.setData(wo);
R
roo00 已提交
78 79 80 81
			return result;
		}
	}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	public static class Wo extends WrapBoolean {
	}

	public static class Wi extends GsonPropertyObject {

		private String oldPassword;
		private String newPassword;
		private String confirmPassword;

		public String getOldPassword() {
			return oldPassword;
		}

		public void setOldPassword(String oldPassword) {
			this.oldPassword = oldPassword;
		}

		public String getConfirmPassword() {
			return confirmPassword;
		}

		public void setConfirmPassword(String confirmPassword) {
			this.confirmPassword = confirmPassword;
		}

		public String getNewPassword() {
			return newPassword;
		}

		public void setNewPassword(String newPassword) {
			this.newPassword = newPassword;
		}

	}

R
roo00 已提交
117
}