UserManagerService.java 20.4 KB
Newer Older
caixiangyi's avatar
caixiangyi 已提交
1 2 3 4 5
package com.x.cms.assemble.control.service;

import java.util.ArrayList;
import java.util.List;

6
import com.x.cms.core.entity.AppInfo;
R
fix  
roo00 已提交
7 8
import org.apache.commons.lang3.StringUtils;

caixiangyi's avatar
caixiangyi 已提交
9 10 11
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.http.EffectivePerson;
NoSubject's avatar
NoSubject 已提交
12
import com.x.base.core.project.organization.Identity;
R
roo00 已提交
13
import com.x.base.core.project.organization.Person;
caixiangyi's avatar
caixiangyi 已提交
14
import com.x.base.core.project.organization.Unit;
R
roo00 已提交
15
import com.x.base.core.project.tools.ListTools;
caixiangyi's avatar
caixiangyi 已提交
16
import com.x.cms.assemble.control.Business;
NoSubject's avatar
NoSubject 已提交
17
import com.x.cms.assemble.control.ThisApplication;
caixiangyi's avatar
caixiangyi 已提交
18 19 20 21 22 23 24

/**
 * 组织人员角色相关信息的服务类
 * 
 * @author O2LEE
 */
public class UserManagerService {
NoSubject's avatar
NoSubject 已提交
25

R
roo00 已提交
26 27
	/**
	 * 根据人员名称获取人员
NoSubject's avatar
NoSubject 已提交
28
	 * 
R
roo00 已提交
29 30 31 32
	 * @param personName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
33
	public Person getPerson(String personName) throws Exception {
R
roo00 已提交
34 35 36 37
		Business business = null;
		Person person = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
38 39 40
			person = business.organization().person().getObject(personName);
			if (person == null) {
				if (personName.endsWith("@P") && personName.split("@P").length == 3) {
41
					return business.organization().person().getObject(personName.split("@")[1]);
R
roo00 已提交
42 43
				}
			}
NoSubject's avatar
NoSubject 已提交
44
			if (person == null) {
45 46
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					return business.organization().person().getObject(personName.split("@")[0]);
R
roo00 已提交
47 48
				}
			}
NoSubject's avatar
NoSubject 已提交
49
		} catch (Exception e) {
R
roo00 已提交
50 51 52 53
			throw e;
		}
		return person;
	}
NoSubject's avatar
NoSubject 已提交
54

caixiangyi's avatar
caixiangyi 已提交
55
	/**
NoSubject's avatar
NoSubject 已提交
56
	 * 根据员工姓名获取组织名称 如果用户有多个身份,则取组织级别最大的组织名称
caixiangyi's avatar
caixiangyi 已提交
57
	 * 
R
update  
roo00 已提交
58
	 * @param personName
caixiangyi's avatar
caixiangyi 已提交
59 60 61
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
62 63
	public String getUnitNameWithPerson(String personName) throws Exception {
		List<String> unitNames = null;
caixiangyi's avatar
caixiangyi 已提交
64 65 66 67 68 69
		Business business = null;
		Integer level = 0;
		String result = null;
		Unit unit = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
70
			unitNames = business.organization().unit().listWithPerson(personName);
71 72 73 74 75 76
			if ( ListTools.isEmpty( unitNames )) {
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					unitNames = business.organization().unit().listWithPerson( personName.split("@")[0] );
				}
			}
			if ( ListTools.isNotEmpty( unitNames )) {
NoSubject's avatar
NoSubject 已提交
77 78 79
				for (String unitName : unitNames) {
					unit = business.organization().unit().getObject(unitName);
					if (level < unit.getLevel()) {
caixiangyi's avatar
caixiangyi 已提交
80 81 82 83 84
						level = unit.getLevel();
						result = unitName;
					}
				}
			}
NoSubject's avatar
NoSubject 已提交
85
		} catch (Exception e) {
caixiangyi's avatar
caixiangyi 已提交
86 87 88 89 90 91 92 93 94 95 96 97
			throw e;
		}
		return result;
	}

	/**
	 * 根据身份名称获取身份所属的组织名称
	 * 
	 * @param identity
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
98
	public String getUnitNameByIdentity(String identity) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
99 100 101
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
102 103 104
			return business.organization().unit().getWithIdentity(identity);
		} catch (NullPointerException e) {
			System.out.println("根据身份获取所属组织名称时发生NullPointerException异常。identity:" + identity);
caixiangyi's avatar
caixiangyi 已提交
105 106
			return null;
		} catch (Exception e) {
NoSubject's avatar
NoSubject 已提交
107
			System.out.println("根据身份获取所属组织名称时发生异常。identity:" + identity);
caixiangyi's avatar
caixiangyi 已提交
108 109 110
			throw e;
		}
	}
NoSubject's avatar
NoSubject 已提交
111

caixiangyi's avatar
caixiangyi 已提交
112 113
	/**
	 * 根据人员姓名获取人员所属的一级组织名称,如果人员有多个身份,则取组织等级最大的身份
NoSubject's avatar
NoSubject 已提交
114
	 * 
caixiangyi's avatar
caixiangyi 已提交
115 116 117 118
	 * @param personName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
119
	public String getTopUnitNameWithPerson(String personName) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
120 121 122 123 124
		String identity = null;
		String topUnitName = null;
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
125
			// 兼容一下传过来的perosnName有可能是个人,有可能是身份
126 127 128 129 130 131 132
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
133 134 135
			identity = getMajorIdentityWithPerson(personName);
			if (identity != null && !identity.isEmpty()) {
				topUnitName = business.organization().unit().getWithIdentityWithLevel(identity, 1);
caixiangyi's avatar
caixiangyi 已提交
136 137
			}
			return topUnitName;
NoSubject's avatar
NoSubject 已提交
138
		} catch (Exception e) {
caixiangyi's avatar
caixiangyi 已提交
139 140 141
			throw e;
		}
	}
NoSubject's avatar
NoSubject 已提交
142

caixiangyi's avatar
caixiangyi 已提交
143 144 145 146 147 148 149
	/**
	 * 根据身份名称获取身份所属的顶层组织名称
	 * 
	 * @param identity
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
150
	public String getTopUnitNameByIdentity(String identity) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
151 152 153
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
154 155
			return business.organization().unit().getWithIdentityWithLevel(identity, 1);
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
156 157 158 159 160 161 162
			return null;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
NoSubject's avatar
NoSubject 已提交
163
	 * 根据个人姓名,根据个人姓名获取主身份
caixiangyi's avatar
caixiangyi 已提交
164 165 166 167 168
	 * 
	 * @param personName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
169
	public String getMajorIdentityWithPerson(String personName) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
170 171 172 173
		List<String> identities = null;
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
174 175 176 177 178 179 180
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
181 182 183 184 185 186 187 188 189
			identities = business.organization().identity().listWithPerson(personName);
			if (identities != null && !identities.isEmpty()) {
				if( identities.size() == 1 ) {
					return identities.get(0);
				}
				for (String identity : identities) {
					Identity obj = business.organization().identity().getObject(identity);
					if (obj.getMajor()) {
						return identity;
caixiangyi's avatar
caixiangyi 已提交
190 191 192
					}
				}
			}
NoSubject's avatar
NoSubject 已提交
193 194
			return null;
		} catch (Exception e) {
caixiangyi's avatar
caixiangyi 已提交
195 196 197 198 199 200 201 202 203 204 205
			throw e;
		}
	}

	/**
	 * 根据用户的身份查询用户的姓名
	 * 
	 * @param identity
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
206 207
	public String getPersonNameWithIdentity(String identity) throws Exception {
		if (StringUtils.isEmpty(identity)) {
caixiangyi's avatar
caixiangyi 已提交
208 209 210 211 212
			throw new Exception("identity is null!");
		}
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
NoSubject's avatar
NoSubject 已提交
213 214
			return business.organization().person().getWithIdentity(identity);
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
215 216 217 218 219 220 221 222
			return null;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 获取人员所属的所有组织名称列表
NoSubject's avatar
NoSubject 已提交
223
	 * 
caixiangyi's avatar
caixiangyi 已提交
224 225 226 227
	 * @param personName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
228
	public List<String> listUnitNamesWithPerson(String personName) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
229 230
		List<String> unitNames = null;
		Business business = null;
NoSubject's avatar
NoSubject 已提交
231
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
caixiangyi's avatar
caixiangyi 已提交
232
			business = new Business(emc);
233 234 235 236 237 238 239
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
240 241 242
			unitNames = business.organization().unit().listWithPersonSupNested(personName);
			return unitNames == null ? new ArrayList<>() : unitNames;
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
243 244 245 246 247
			return null;
		} catch (Exception e) {
			throw e;
		}
	}
NoSubject's avatar
NoSubject 已提交
248

caixiangyi's avatar
caixiangyi 已提交
249 250 251
	/**
	 * 根据用户姓名查询用户所有的身份信息
	 * 
252
	 * @param personName
caixiangyi's avatar
caixiangyi 已提交
253 254 255
	 * @return
	 * @throws Exception
	 */
256 257
	public List<String> listIdentitiesWithPerson(String personName) throws Exception {
		if (StringUtils.isEmpty(personName)) {
caixiangyi's avatar
caixiangyi 已提交
258 259 260
			throw new Exception("userName is null!");
		}
		Business business = null;
NoSubject's avatar
NoSubject 已提交
261
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
caixiangyi's avatar
caixiangyi 已提交
262
			business = new Business(emc);
263 264 265 266 267 268 269 270
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
			return business.organization().identity().listWithPerson(personName);
NoSubject's avatar
NoSubject 已提交
271
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
272 273 274 275 276 277 278 279
			return null;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 列示人员所拥有的所有角色信息
NoSubject's avatar
NoSubject 已提交
280
	 * 
R
update  
roo00 已提交
281
	 * @param personName
caixiangyi's avatar
caixiangyi 已提交
282 283 284
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
285
	public List<String> listRoleNamesByPerson(String personName) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
286 287 288 289 290
		Business business = null;
		List<String> roleList = null;
		List<String> nameList = new ArrayList<String>();
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
291 292 293 294 295 296 297
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
298
			roleList = business.organization().role().listWithPerson(personName);
caixiangyi's avatar
caixiangyi 已提交
299
			if (roleList != null && roleList.size() > 0) {
NoSubject's avatar
NoSubject 已提交
300
				roleList.stream().filter(r -> !nameList.contains(r)).distinct().forEach(r -> nameList.add(r));
caixiangyi's avatar
caixiangyi 已提交
301
			}
NoSubject's avatar
NoSubject 已提交
302
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
303 304 305 306
			return null;
		} catch (Exception e) {
			throw e;
		}
NoSubject's avatar
NoSubject 已提交
307
		return nameList == null ? new ArrayList<>() : nameList;
caixiangyi's avatar
caixiangyi 已提交
308 309 310 311
	}

	/**
	 * 列示人员所拥有的所有群组信息
NoSubject's avatar
NoSubject 已提交
312
	 * 
caixiangyi's avatar
caixiangyi 已提交
313 314 315 316 317 318 319 320 321 322
	 * @param personName
	 * @return
	 * @throws Exception
	 */
	public List<String> listGroupNamesByPerson(String personName) throws Exception {
		Business business = null;
		List<String> groupList = null;
		List<String> nameList = new ArrayList<String>();
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
323 324 325 326 327 328 329
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
330
			groupList = business.organization().group().listWithPerson(personName);
caixiangyi's avatar
caixiangyi 已提交
331
			if (groupList != null && groupList.size() > 0) {
NoSubject's avatar
NoSubject 已提交
332
				groupList.stream().filter(g -> !nameList.contains(g)).distinct().forEach(g -> nameList.add(g));
caixiangyi's avatar
caixiangyi 已提交
333
			}
NoSubject's avatar
NoSubject 已提交
334
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
335 336 337 338
			return null;
		} catch (Exception e) {
			throw e;
		}
NoSubject's avatar
NoSubject 已提交
339
		return nameList == null ? new ArrayList<>() : nameList;
caixiangyi's avatar
caixiangyi 已提交
340
	}
NoSubject's avatar
NoSubject 已提交
341

caixiangyi's avatar
caixiangyi 已提交
342 343 344
	/**
	 * 判断用户是否有指定的平台角色,比如CMS系统管理员
	 * 
R
update  
roo00 已提交
345 346
	 * @param personName
	 * @param roleName
caixiangyi's avatar
caixiangyi 已提交
347 348 349
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
350 351
	public boolean isHasPlatformRole(String personName, String roleName) throws Exception {
		if (StringUtils.isEmpty(personName)) {
caixiangyi's avatar
caixiangyi 已提交
352 353
			throw new Exception("personName is null!");
		}
NoSubject's avatar
NoSubject 已提交
354
		if (StringUtils.isEmpty(roleName)) {
caixiangyi's avatar
caixiangyi 已提交
355 356 357 358 359 360
			throw new Exception("roleName is null!");
		}
		List<String> roleList = null;
		Business business = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			business = new Business(emc);
361 362 363 364 365 366 367
			if( StringUtils.isNotEmpty( personName )){
				if (personName.endsWith("@P") && personName.split("@P").length == 2) {
					personName = business.organization().person().get(personName.split("@")[0]);
				}else{
					personName = business.organization().person().get(personName);
				}
			}
NoSubject's avatar
NoSubject 已提交
368
			roleList = business.organization().role().listWithPerson(personName);
caixiangyi's avatar
caixiangyi 已提交
369
			if (roleList != null && !roleList.isEmpty()) {
NoSubject's avatar
NoSubject 已提交
370
				if (roleList.stream().filter(r -> roleName.equalsIgnoreCase(r)).count() > 0) {
caixiangyi's avatar
caixiangyi 已提交
371 372 373 374 375
					return true;
				}
			} else {
				return false;
			}
NoSubject's avatar
NoSubject 已提交
376
		} catch (NullPointerException e) {
caixiangyi's avatar
caixiangyi 已提交
377 378 379 380 381 382 383
			return false;
		} catch (Exception e) {
			throw e;
		}
		return false;
	}

R
update  
roo00 已提交
384
	/**
NoSubject's avatar
NoSubject 已提交
385 386
	 * 判断指定人员是否是管理员 1、xadmin 2、拥有manager角色 3、拥有CMSManager@CMSManagerSystemRole@R角色
	 * 
R
update  
roo00 已提交
387 388 389 390
	 * @param effectivePerson
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
391
	public Boolean isManager(EffectivePerson effectivePerson) throws Exception {
R
update  
roo00 已提交
392
		if (effectivePerson == null) {
NoSubject's avatar
NoSubject 已提交
393
			throw new Exception("effectivePerson is null!");
caixiangyi's avatar
caixiangyi 已提交
394
		}
NoSubject's avatar
NoSubject 已提交
395
		if (effectivePerson.isManager()) {
R
update  
roo00 已提交
396 397
			return true;
		}
NoSubject's avatar
NoSubject 已提交
398
		if (effectivePerson.isManager() || effectivePerson.isCipher()) {
R
update  
roo00 已提交
399
			return true;
caixiangyi's avatar
caixiangyi 已提交
400
		}
NoSubject's avatar
NoSubject 已提交
401
		if (this.isHasPlatformRole(effectivePerson.getDistinguishedName(), ThisApplication.ROLE_CMSManager)) {
R
update  
roo00 已提交
402 403 404
			return true;
		}
		return false;
caixiangyi's avatar
caixiangyi 已提交
405
	}
NoSubject's avatar
NoSubject 已提交
406 407

	public String getPersonIdentity(String personName, String identity) throws Exception {
caixiangyi's avatar
caixiangyi 已提交
408 409 410
		List<String> identityNames = null;
		String identityName = null;
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
NoSubject's avatar
NoSubject 已提交
411
			identityNames = listIdentitiesWithPerson(personName);
R
roo00 已提交
412
//			LogUtil.INFO("identityNames:", identityNames );
NoSubject's avatar
NoSubject 已提交
413 414 415 416
			if (StringUtils.isEmpty(identity)) {
				if (identityNames.size() == 0) {
					throw new Exception("perons has no identity. personName:" + personName);
				} else if (identityNames.size() > 0) {
caixiangyi's avatar
caixiangyi 已提交
417 418
					identityName = identityNames.get(0);
				}
NoSubject's avatar
NoSubject 已提交
419 420 421 422
			} else {
				// 判断传入的身份是否合法
				identityName = this.findIdentity(identityNames, identity);
				if (StringUtils.isEmpty(identity)) {
R
roo00 已提交
423
					identityName = identityNames.get(0);
caixiangyi's avatar
caixiangyi 已提交
424 425 426 427 428 429 430
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return identityName;
	}
R
roo00 已提交
431

NoSubject's avatar
NoSubject 已提交
432 433 434 435 436
	private String findIdentity(List<String> identityNames, String identityName) throws Exception {
		if (identityName != null && !identityName.isEmpty() && !"null".equals(identityName)) {
			if (identityNames != null && !identityNames.isEmpty()) {
				for (String identity : identityNames) {
					if (identity.equalsIgnoreCase(identityName)) {
caixiangyi's avatar
caixiangyi 已提交
437 438 439 440
						return identity;
					}
				}
			}
NoSubject's avatar
NoSubject 已提交
441 442
		} else {
			if (identityNames != null && !identityNames.isEmpty()) {
caixiangyi's avatar
caixiangyi 已提交
443 444 445 446 447 448
				return identityNames.get(0);
			}
		}
		return null;
	}

R
roo00 已提交
449
	/**
450 451
	 * 根据组织名称,查询组织内所有的人员标识,包括下级组织<br/>
	 * 2020-06-12 改为使用唯一标识查询<br/>
R
roo00 已提交
452 453 454 455
	 * @param unitName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
456
	public List<String> listPersonWithUnit(String unitName) throws Exception {
457
		if (StringUtils.isEmpty( unitName )) {
R
roo00 已提交
458 459
			throw new Exception("unitName is empty!");
		}
NoSubject's avatar
NoSubject 已提交
460 461
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
462 463 464 465 466

			//2020-06-12 unitName可能有3段,可能有2段,统一使用中间的唯一标识来进行查询
			String unique = getUniqueWithName( unitName );

			return business.organization().person().listWithUnitSubNested( unique );
R
roo00 已提交
467 468 469 470 471 472
		} catch (Exception e) {
			throw e;
		}
	}

	/**
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	 * 获取组织对象的唯一标识<br/>
	 *<br/>
	 * 组织对象标识一般会有3段,如 综合部@1304-73504398-13419-0347@U, 张三@293041-9305983-04258-0943@P<br/>
	 * 文档权限里也会存在2段,因为第一段经常会变,如组织:行政综合部@1304-73504398-13419-0347@U<br/>
	 * 所以查询的时候最好只用中间的唯一标识来查询<br/>
	 * @param orgObjectName
	 * @return
	 */
	private String getUniqueWithName(String orgObjectName ) {
		if( StringUtils.isNotEmpty( orgObjectName )){
			String[] array = orgObjectName.split("@");
			if( array.length == 3 ){
				return array[1];
			}else if( array.length == 2 ){
				return array[0];
			}else{
				return orgObjectName;
			}
		}
		return null;
	}

	/**
	 * 根据群组名称,查询群组内所有的人员标识<br/>
	 * 2020-06-12 改为使用唯一标识查询<br/>
	 *
R
roo00 已提交
499 500 501 502
	 * @param groupName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
503 504
	public List<String> listPersonWithGroup(String groupName) throws Exception {
		if (StringUtils.isEmpty(groupName)) {
R
roo00 已提交
505 506
			throw new Exception("groupName is empty!");
		}
NoSubject's avatar
NoSubject 已提交
507 508
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
509 510 511 512 513

			//2020-06-12 unitName可能有3段,可能有2段,统一使用中间的唯一标识来进行查询
			String unique = getUniqueWithName( groupName );

			return business.organization().person().listWithGroup( unique );
R
roo00 已提交
514 515 516 517 518
		} catch (Exception e) {
			throw e;
		}
	}

519 520 521 522 523 524 525
	/**
	 * 根据角色名称,查询角色成员内所有的人员标识<br/>
	 * 2020-06-12 改为使用唯一标识查询<br/>
	 * @param role
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
526 527
	public List<String> listPersonWithRole(String role) throws Exception {
		if (StringUtils.isEmpty(role)) {
R
roo00 已提交
528 529
			throw new Exception("role is empty!");
		}
NoSubject's avatar
NoSubject 已提交
530 531
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
532 533 534 535 536

			//2020-06-12 unitName可能有3段,可能有2段,统一使用中间的唯一标识来进行查询
			String unique = getUniqueWithName( role );

			return business.organization().person().listWithRole( unique );
R
roo00 已提交
537 538 539 540 541 542 543
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 查询系统内所有顶级组织的数量
NoSubject's avatar
NoSubject 已提交
544
	 * 
R
roo00 已提交
545 546 547 548
	 * @return
	 * @throws Exception
	 */
	public int countTopUnit() throws Exception {
NoSubject's avatar
NoSubject 已提交
549 550
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
R
roo00 已提交
551
			List<String> unitNames = business.organization().unit().listWithLevel(1);
NoSubject's avatar
NoSubject 已提交
552
			if (ListTools.isNotEmpty(unitNames)) {
R
roo00 已提交
553 554 555 556 557 558 559 560 561 562
				return unitNames.size();
			}
		} catch (Exception e) {
			throw e;
		}
		return 0;
	}

	/**
	 * 判断指定组织是否是顶级组织
NoSubject's avatar
NoSubject 已提交
563
	 * 
R
roo00 已提交
564 565 566 567
	 * @param unitName
	 * @return
	 * @throws Exception
	 */
NoSubject's avatar
NoSubject 已提交
568 569
	public boolean isTopUnit(String unitName) throws Exception {
		if (StringUtils.isEmpty(unitName)) {
R
roo00 已提交
570 571
			throw new Exception("unitName is empty!");
		}
NoSubject's avatar
NoSubject 已提交
572 573
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
R
roo00 已提交
574
			List<String> unitNames = business.organization().unit().listWithLevel(1);
NoSubject's avatar
NoSubject 已提交
575
			if (ListTools.isNotEmpty(unitNames) && unitNames.contains(unitName)) {
R
roo00 已提交
576 577 578 579 580 581 582
				return true;
			}
		} catch (Exception e) {
			throw e;
		}
		return false;
	}
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

	public boolean hasCategoryManagerPermission( EffectivePerson person, String appId) throws Exception {
		//xadmin或者Cipher
		if( person.isManager() || person.isCipher() ){
			return true;
		}
		if( StringUtils.equalsIgnoreCase("xadmin", person.getName() ) || StringUtils.equalsIgnoreCase("xadmin", person.getDistinguishedName() ) ){
			return true;
		}
		UserManagerService userManagerService = new UserManagerService();
		//Manager管理员
		if( userManagerService.isHasPlatformRole( person.getDistinguishedName(), ThisApplication.ROLE_Manager )){
			return true;
		}
		//CMS管理员
		if( userManagerService.isHasPlatformRole( person.getDistinguishedName(), ThisApplication.ROLE_CMSManager )){
			return true;
		}

		//查询用户是否为该栏目的管理者
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			AppInfo appInfo = emc.find( appId, AppInfo.class );
			//是管理员
			if( ListTools.isNotEmpty(appInfo.getManageablePersonList()) && ListTools.contains( appInfo.getManageablePersonList(), person.getDistinguishedName() )){
				return true;
			}
			if( ListTools.isNotEmpty( appInfo.getManageableUnitList() )){
				List<String> unitNames = userManagerService.listUnitNamesWithPerson( person.getDistinguishedName() );
				if( ListTools.isNotEmpty( unitNames )){
					unitNames.retainAll( appInfo.getManageableUnitList() );
					if( ListTools.isNotEmpty( unitNames )){
						return true;
					}
				}
			}
			if( ListTools.isNotEmpty( appInfo.getManageableGroupList() )){
				List<String> groupNames = userManagerService.listGroupNamesByPerson( person.getDistinguishedName() );
				if( ListTools.isNotEmpty( groupNames )){
					groupNames.retainAll( appInfo.getManageableGroupList() );
					if( ListTools.isNotEmpty( groupNames )){
						return true;
					}
				}
			}
		} catch (Exception e) {
			throw e;
		}

		return false;
	}

	public boolean hasAppInfoManagerPermission( EffectivePerson person ) throws Exception {
		//系统管理员
		if( person.isManager() || person.isCipher() ){
			return true;
		}
		if( StringUtils.equalsIgnoreCase("xadmin", person.getName() ) || StringUtils.equalsIgnoreCase("xadmin", person.getDistinguishedName() ) ){
			return true;
		}
		//CMS管理员
		UserManagerService userManagerService = new UserManagerService();
		if( userManagerService.isHasPlatformRole( person.getDistinguishedName(), ThisApplication.ROLE_CMSManager )){
			return true;
		}
		return false;
	}
caixiangyi's avatar
caixiangyi 已提交
649
}