View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-12-6 下午03:00:14
4    */
5   package gboat2.web.business.impl;
6   
7   import gboat2.base.bridge.model.UserSession;
8   import gboat2.base.bridge.util.PropertiesUtil;
9   import gboat2.base.core.logging.IBusinessLogService;
10  import gboat2.base.core.logging.Level;
11  import gboat2.base.core.model.Parameter;
12  import gboat2.base.core.service.BaseService;
13  import gboat2.web.Constants;
14  import gboat2.web.business.IAuthorityBusiness;
15  import gboat2.web.business.ILoginBusiness;
16  import gboat2.web.model.AuthorityResourceVO;
17  import gboat2.web.model.Group;
18  import gboat2.web.model.ShortcutResourceVO;
19  import gboat2.web.model.SystemConfig;
20  import gboat2.web.model.User;
21  import gboat2.web.model.UserGroupOrganMapper;
22  import gboat2.web.util.EncryptUtil;
23  import gboat2.web.util.ParameterUtil;
24  
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.LinkedList;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  import org.apache.commons.lang3.StringUtils;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.stereotype.Service;
35  import org.springframework.transaction.annotation.Transactional;
36  /**
37   * 
38   * 登陆业务类
39   * @author sunpf
40   * @since jdk1.6
41   * @date 2012-12-6
42   */
43  
44  @Transactional
45  @Service
46  public class LoginBusinessImpl extends BaseService implements ILoginBusiness {
47  	
48  	private static String LOGIN_CONFIG_FILE = "content/config/login.properties";
49  	private static String VALIDATE_USER_ORGANCODE = "gbmp.validate.user.organCode";
50  	private static String VALIDATE_SYSTEM_NAME = "GBMP";
51  	
52  	@Autowired
53  	private IBusinessLogService loggerService;//日志服务
54  	
55  	@Autowired
56  	private IAuthorityBusiness authorityService;// 权限服务
57  	
58  	
59  	@Override
60  	public String validateSuperUser(String password) {
61  		
62  		String errorMessage = null;
63  		
64  		String jsonOfPlatformParameter =  "{'desc':'定制super用户密码','groupName':'G2平台参数','code':'CODE_SUPER_PASSWORD','name':'super用户密码','value':'super'}";
65  		Parameter parameter = ParameterUtil.getParameter(jsonOfPlatformParameter);
66  		//进行md5加密后与页面传来的密码比较
67  		String md5Value =  EncryptUtil.md5(parameter.getValue());
68  		
69  		if( !md5Value.equals(password)){//super用户,密码不匹配
70  			errorMessage = Constants.ERROR_MESSAGE_WRONG_PASSWORD;
71  		}	
72  			 
73  		return errorMessage;
74  	}
75  
76  	@Override
77  	public String validateUser(String username, User user, String pwd, List<UserGroupOrganMapper> status) {
78  		String errorMessage = null;
79  		  
80  	  if (user == null) { 
81  			errorMessage = Constants.ERROR_MESSAGE_NO_USER;
82  		} else if (status == null || status.size() == 0) {//没有身份
83  			errorMessage = Constants.ERROR_MESSAGE_NO_AUTHORITY;
84  		} else if (user.getStatus() != null && user.getStatus().equals("Y")) {//用户被禁用
85  			errorMessage = Constants.ERROR_MESSAGE_DISABLED_USER;
86  		} else if (user.getEndTime() != null && user.getEndTime().compareTo(new Date()) == -1) {//用户过期
87  			errorMessage = Constants.ERROR_MESSAGE_OVER_TIME_USER;
88  		}
89  		return errorMessage;
90  	}
91  	
92  	
93  	@Override
94  	public UserSession superSession(UserSession userSession, String systemId) {
95  		UserSession session = new UserSession();
96  		session.setSystemId(systemId);
97  		session.setUserId(UserSession.SUPER_ID);
98  		session.setLoginId(Constants.SUPER);
99  		session.setUserNameZh(Constants.SUPER_NAME);
100 		// loggerService.log(Level.DEBUG, session, "用户登录", "用户" + Constants.SUPER + "登录");
101 		loggerService.log(Level.DEBUG, "用户登录", "用户" + Constants.SUPER + "登录");
102 		return session;
103 	}
104 	
105 	@SuppressWarnings("unchecked")
106     @Override
107 	public List<ShortcutResourceVO> showResourceShortcutByAuth(List<AuthorityResourceVO> list) {
108 		StringBuilder resids = new StringBuilder();
109 		List<String> settedIds = new LinkedList<String>();
110 		for (AuthorityResourceVO auth : list) {
111 			if (!settedIds.contains(auth.getResId())) {
112 				if (resids.length() != 0) {
113 					resids.append(",");
114 				}
115 				resids.append("'").append(auth.getResId()).append("'");
116 				settedIds.add(auth.getResId());
117 			}
118 		}
119 		String[][] params = { { ShortcutResourceVO.class.getName() }, { "_userId_null", "is null" },
120 		        { "_resId_in", resids.toString() } };
121 		return (List<ShortcutResourceVO>) authorityService.query(params);
122 	}
123 	
124 	@Override
125 	public ShortcutResourceVO addModule() {
126 		ShortcutResourceVO shortResVO = new ShortcutResourceVO();
127 		shortResVO.setName(Constants.ADD_MODULE_SHORTCUT_NAME);
128 		shortResVO.setResUrl(Constants.ADD_MODULE_SHORTCUT_RESOURCEURL);
129 		shortResVO.setIconUrl(Constants.ADD_MODULE_SHORTCUT_ICONURL);
130 		return shortResVO;
131 	}
132 	
133 	@Override
134 	public Boolean isVerifySuccess(String organId){
135 		return true;
136 //		String qualStatus = this.getQualStatusOfEnterprise(organId);
137 //		if(StringUtils.isEmpty(qualStatus)){
138 //			return false;
139 //		}
140 //		return "VALID".equals(qualStatus.trim());
141 	}
142 
143 	@Override
144 	public Boolean isNeedToVerify(String groupId,String systemId) {
145 	    Properties prop = PropertiesUtil.loadPropertiesFromResource(LOGIN_CONFIG_FILE, this.getClass().getClassLoader());
146 		if(prop != null){
147 			Boolean isNeedToVerifyOfGroup = this.isNeedToVerifyOfGroup(prop,groupId);
148 			Boolean isNeedToVerifyOfSystem = this.isNeedToVerifyOfSystem(prop,systemId);
149 			if(isNeedToVerifyOfGroup && isNeedToVerifyOfSystem){
150 				return true;
151 			}
152 		}
153 		return false;
154 	}
155 	
156 	/**
157 	 * 根据用户组判断其是否需要进行有效性验证
158 	 * 用于切换系统时验证
159 	 * @param prop
160 	 * @param groupId
161 	 * @return
162 	 */
163 	@Override
164 	public Boolean isNeedToVerifyByGroup(String groupId) {
165 	    Properties prop = PropertiesUtil.loadPropertiesFromResource(LOGIN_CONFIG_FILE, this.getClass().getClassLoader());
166 		if(prop != null){
167 			return this.isNeedToVerifyOfGroup(prop,groupId);
168 		}
169 		return false;
170 	}
171 	
172 	/**
173 	 * 根据用户组判断其是否需要进行有效性验证
174 	 * @param prop
175 	 * @param groupId
176 	 * @return
177 	 */
178 	private Boolean isNeedToVerifyOfGroup(Properties prop,String groupId) {
179 		String needToVerifyGroup = prop.getProperty(VALIDATE_USER_ORGANCODE);
180 		if(StringUtils.isNotEmpty(needToVerifyGroup)){
181 			Group group = (Group) this.get(Group.class, groupId);
182 			if(group != null && StringUtils.isNotEmpty(group.getGroupCode()) 
183 					&& needToVerifyGroup.contains(group.getGroupCode().trim())){
184 				return true;
185 			}
186 		}
187 		return false;
188 	}
189 	
190 	/**
191 	 * 根据系统判断其是否需要进行有效性验证
192 	 * @param prop
193 	 * @param systemId
194 	 * @return
195 	 */
196 	private Boolean isNeedToVerifyOfSystem(Properties prop, String systemId){
197 		String needToVerifySystem = prop.getProperty(VALIDATE_SYSTEM_NAME);
198 		if(StringUtils.isNotEmpty(needToVerifySystem)){
199 			SystemConfig systemConfig = (SystemConfig) this.get(SystemConfig.class, systemId);
200 			if(systemConfig != null  && StringUtils.isNotEmpty(systemConfig.getLoginBundleName())
201 					&& needToVerifySystem.trim().equals(systemConfig.getLoginBundleName().trim())){
202 				return true;
203 			}
204 		}
205 		return false;
206 	}
207 	
208 	/**
209 	 * 获取企业招投标资质
210 	 * @return
211 	 */
212 	private String getQualStatusOfEnterprise(String organId){
213 		Map<String, Object> params = new HashMap<String, Object>();
214 		String hql = "select qualStatus from Enterprise as e where e.enterpriseId = :enterpriseId";
215 		params.put("enterpriseId", organId);
216 		Object qualStatus = (Object) baseDAO.get(hql ,params);
217 		if(qualStatus != null){
218 			return qualStatus.toString();
219 		}
220 	    return "";
221 	}
222 
223 }