ActionLogin.java 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
package com.x.okr.assemble.control.jaxrs.login;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.okr.assemble.control.OkrUserCache;
import com.x.okr.assemble.control.jaxrs.login.exception.ExceptionGetOkrUserCache;
import com.x.okr.assemble.control.jaxrs.login.exception.ExceptionUserHasNoProxyIdentity;
import com.x.okr.assemble.control.jaxrs.login.exception.ExceptionUserIdentityQuery;
import com.x.okr.assemble.control.jaxrs.login.exception.ExceptionUserProxyQuery;

public class ActionLogin extends BaseAction {

	private static  Logger logger = LoggerFactory.getLogger( ActionLogin.class );
	
	protected ActionResult<OkrUserCache> execute( HttpServletRequest request, EffectivePerson effectivePerson, JsonElement jsonElement ) throws Exception {
		ActionResult<OkrUserCache> result = new ActionResult<>();
		List<String> ids = null;
		String identity = null;
		OkrUserCache okrUserCache = null;
		boolean hasIdentity = false;
		Wi wrapIn = null;
		Boolean check = true;

		if (check) {
			try {//获取传入的参数,主要是loginIdentity(登录身份)
				wrapIn = this.convertToWrapIn( jsonElement, Wi.class);
			} catch (Exception e) {
				wrapIn = new Wi();
			}
		}
		
		if (wrapIn != null) {
			if ( StringUtils.isNotEmpty(wrapIn.getLoginIdentity()) ) {
				try { //查询用户是否拥有传入的身份名称
					hasIdentity = okrUserManagerService.hasIdentity( effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity() );
				} catch (Exception e) {
					check = false;
					Exception exception = new ExceptionUserProxyQuery(e, effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity() );
					result.error( exception );
					logger.error( e, effectivePerson, request, null);
				}
				if ( hasIdentity ) {//用户拥有传入的身份, 以指定的身份登录
					try {
						okrUserCache = setUserLoginIdentity( request, effectivePerson, wrapIn.getLoginIdentity() );
						result.setData(okrUserCache);
					} catch (Exception e) {
						Exception exception = new ExceptionGetOkrUserCache(e, effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
						result.error(exception);
						logger.error(e, effectivePerson, request, null);
					}
				} else {// 用户自己没有传入的身份,查询用户是否有该身份的代理配置(秘书)
					try {
						ids = okrConfigSecretaryService.listIdsByLeaderIdentity(effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
						if (ListTools.isEmpty(ids)) {//用户没有指定身份的代理权限,无法登录系统							
							//以用户自己的主要身份进行登录
							identity = okrUserManagerService.getIdentityWithPerson( effectivePerson.getDistinguishedName() );
							if( StringUtils.isNotEmpty( identity )) {
								System.out.println( "用户使用身份:" + identity + " 登录系统......" );
								try {
									okrUserCache = setUserLoginIdentity( request, effectivePerson, identity );
									result.setData(okrUserCache);
								} catch (Exception e) {
									check = false;
									Exception exception = new ExceptionGetOkrUserCache(e, effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
									result.error(exception);
									logger.error(e, effectivePerson, request, null);
								}
							}else {
								check = false;
								Exception exception = new ExceptionUserHasNoProxyIdentity( effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
								result.error(exception);
							}							
						} else {// 顺利查询到身份代理配置信息,向cache里新增用户相关信息
							okrUserCache = setUserLoginIdentity(request, effectivePerson, wrapIn.getLoginIdentity());
							result.setData(okrUserCache);
						}
					} catch (Exception e) {
						check = false;
						Exception exception = new ExceptionUserProxyQuery(e, effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
						result.error(exception);
						logger.error(e, effectivePerson, request, null);
					}
				}
			} else { // 没有传入身份,即以自己的第一个身份登入系统
				if (!"xadmin".equals( effectivePerson.getDistinguishedName().toLowerCase()) ) {//查询是不是xadmin用户,如果不是xadmin,则查询自己的第一个身份
					try {
						identity = okrUserManagerService.getIdentityWithPerson( effectivePerson.getDistinguishedName() );
						if (identity != null && !identity.isEmpty()) {
							System.out.println( "用户使用身份:" + identity + " 登录系统......" );
							okrUserCache = setUserLoginIdentity( request, effectivePerson, identity );
						}
					} catch (Exception e) {
						check = false;
						Exception exception = new ExceptionUserIdentityQuery(e, effectivePerson.getDistinguishedName());
						result.error(exception);
						logger.error(e, effectivePerson, request, null);
					}
				} else {
					try {
						okrUserCache = setUserXadmin(request, effectivePerson);
					} catch (Exception e) {
						Exception exception = new ExceptionGetOkrUserCache(e, effectivePerson.getDistinguishedName(), wrapIn.getLoginIdentity());
						result.error(exception);
						logger.error(e, effectivePerson, request, null);
					}
				}
				result.setData( okrUserCache );
			}
			
			if( check &&  okrUserCache!= null ) {//判断是否拥有工作管理权限
				if( effectivePerson.isManager() ) {
					okrUserCache.setOkrManager( true );
				}
				if(okrUserManagerService.isHasPlatformRole(okrUserCache.getLoginUserName(), "OKRManager")) {
					okrUserCache.setOkrManager( true );
				}
				if(okrUserManagerService.isOkrWorkManager(okrUserCache.getLoginIdentityName())) {
					okrUserCache.setOkrManager( true );
				}
			}
		} else {
			check = false;
			result.error(new Exception("请求传入的参数'wrapIn'为空,无法继续登录应用!"));
		}
		
		return result;
	}
	
	public static class Wi extends GsonPropertyObject implements Serializable{
		private static final long serialVersionUID = -5076990764713538973L;
		
		@FieldDescribe( "登录身份名称" )
		private String loginIdentity = null;
		public String getLoginIdentity() {
			return loginIdentity;
		}
		public void setLoginIdentity(String loginIdentity) {
			this.loginIdentity = loginIdentity;
		}
	}

}