package foundation.user; import java.util.List; import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import foundation.capacity.Actor; import foundation.capacity.ActorBucket; import foundation.capacity.ActorTarget; import foundation.capacity.ActorType; import foundation.capacity.ActorsRuntime; import foundation.capacity.sight.Sight; import foundation.dao.Filter; import foundation.data.entity.Entity; import foundation.data.entity.EntitySet; import foundation.data.object.DataObject; import foundation.data.object.EntitySaver; import foundation.json.IJSONProvider; import foundation.json.IJSONWriter; import foundation.json.JSONReader; import foundation.org.Employee; import foundation.org.Employees; import foundation.org.Org; import foundation.org.OrgAccount; import foundation.org.Position; import foundation.token.IOnlineUser; import foundation.token.IUser; import foundation.util.CaseInsensitiveSet; import foundation.util.MapList; import foundation.variant.provider.DataEvent; import foundation.variant.provider.VariantProviderType; public class User extends IUser implements IJSONProvider { protected static Logger logger; private static Set adminSet; private String id; private String name; private String password; private boolean passNeedChange; private String remark; private String employeeId; private String orgId; private Org org; private Employees employees; private MapList positions; private MapList actors; private ActorsRuntime actorsRuntime; private boolean built; static { logger = LogManager.getLogger(User.class); adminSet = new CaseInsensitiveSet(); adminSet.add("admin"); } public User() { actors = new MapList(); actorsRuntime = new ActorsRuntime(); passNeedChange = false; built = false; } public static User getInstance(String userId) { UserBucket userBucket = UserBucket.getInstance(); User user = userBucket.getOrLoad(userId); return user; } public void load(Entity entity) { id = entity.getString("id"); name = entity.getString("name"); password = entity.getString("password"); passNeedChange = entity.getBoolean("pass_need_change", true); remark = entity.getString("remark"); orgId = entity.getString("org_id"); } public void loadHierarchyAndCapacitys() { try { actorsRuntime.clear(); //1. 加载公司、员工、岗位 loadHierarchys(); //2. 获取角色列表,创建Runtime collectActors(); built = true; } catch (Exception e) { e.printStackTrace(); } } private void loadHierarchys() throws Exception { //1. 加载用户关联的公司 org = Org.getInstance(orgId); //2. 加载用户关联的员工(包含员工关联的岗位) employees = Employees.getInstance(id); positions = employees.getPositions(); } private void collectActors() throws Exception { //1. 获取员工自己的权限 doCollectActors(ActorType.User, this, id); //2. 加载员工的角色 for(Employee employee : employees) { doCollectActors(ActorType.Employee, employee, employee.getId()); } //3. 加载岗位权限 if (positions != null) { for (Position position: positions) { doCollectActors(ActorType.Position, position, position.getId()); } } //4. 加载授予给公司的角色(以后这里要去掉,先给公司重的管理员,然后再分配) if (org != null) { List orgAccounts = org.getOrgAccounts(); if (orgAccounts != null) { for (OrgAccount orgAccount: orgAccounts) { doCollectActors(ActorType.OrgAccount, orgAccount, orgAccount.getId()); } } } } private void doCollectActors(ActorType type, Object sender, String targetId) throws Exception { //1. 获取 Actor 配置 DataObject dataObject = DataObject.getInstance("sys_right_actor_target"); Filter filter = new Filter("target_id", targetId); EntitySet entitySet = dataObject.getTableEntitySet(filter); if (entitySet.isEmpty()) { return; } //2. 加载 Actor ActorBucket actorBucket = ActorBucket.getInstance(); for (Entity entity: entitySet) { ActorTarget target = new ActorTarget(); target.load(entity); String actorId = entity.getString("actor_id"); Actor actor = actorBucket.get(actorId); if (actor == null) { logger.error("actor not exists: {}", actorId); continue; } target.setActor(actor); if (ActorType.Position == type) { Position position = (Position)sender; target.setPosition(position); } else if (ActorType.OrgAccount == type) { target.loadAccount(); } actors.add(target.getId(), target); actorsRuntime.loadOne(target); } } public String getPassword() { return password; } public synchronized void chagnePassword(String newPass) throws Exception { DataObject dataObject = DataObject.getInstance("sys_user"); EntitySaver saver = dataObject.createEntitySaver(id); saver.set("password", newPass); saver.set("pass_need_change", "F"); saver.update(); password = newPass; passNeedChange = false; } public void checkBuild() { if (built) { return; } synchronized (this) { if (built) { return; } loadHierarchyAndCapacitys(); } } public boolean isOrgFrozen() { if (org == null) { return false; } return org.isFrozen(); } public boolean isAdmin() { return adminSet.contains(name); } public String getId() { return id; } public String getName() { return name; } public String getRemark() { return remark; } @Override public String getEmployeeId() { Employee employee = getCurrentEmployee(); if (employee != null) { return employee.getId(); } return null; } @Override public String getEmployeeName() { Employee employee = getCurrentEmployee(); if (employee != null) { return employee.getName(); } return name; } public String getOrgId() { return orgId; } public Org getOrg() { return org; } public Sight getCurrentSight(String onlineCode) throws Exception { //1. 如果没有计算角色,先计算角色(如果计算不会执行) checkBuild(); //2. ActorTarget actorTarget = actorsRuntime.getCurrent(onlineCode); if (actorTarget == null) { return null; } return actorTarget.getSightRuntime(); } public ActorTarget getCurrentActor(String onlineCode) throws Exception { //1. 如果没有计算角色,先计算角色(如果计算不会执行) checkBuild(); //2. return actorsRuntime.getCurrent(onlineCode); } public ActorTarget changeCurrentActor(String onlineCode, String actorId) throws Exception { //1. 如果没有计算角色,先计算角色(如果计算不会执行) checkBuild(); //2. return actorsRuntime.changeCurrent(onlineCode, actorId); } public Position getPosition(String onlineCode) throws Exception { //1. 如果没有计算角色,先计算角色(如果计算不会执行) checkBuild(); //2. ActorTarget actorRuntime = actorsRuntime.getCurrent(onlineCode); return actorRuntime.getPosition(); } public ActorTarget getCapacity(String actorId) { return actorsRuntime.get(actorId); } public void saveCapacitys(JSONReader capacitys) { actorsRuntime.saveChanges(capacitys); } public ActorsRuntime getActorsRuntime() { return actorsRuntime; } @Override public boolean isAnymous() { return false; } @Override public String getProviderName() { return ProviderName; } @Override public VariantProviderType getProviderType() { return VariantProviderType.Transiant; } @Override public Object getVariantValue(DataEvent dataEvent, String variantName) { if ("user.id".equalsIgnoreCase(variantName)) { return id; } else if ("user.name".equalsIgnoreCase(variantName)) { return name; } else if ("user.employee_id".equalsIgnoreCase(variantName)) { Employee employee = getCurrentEmployee(); if (employee != null) { return employee.getId(); } return id; } else if ("user.employee_code".equalsIgnoreCase(variantName)) { Employee employee = getCurrentEmployee(); if (employee != null) { return employee.getCode(); } return null; } else if ("user.employee_name".equalsIgnoreCase(variantName)) { Employee employee = getCurrentEmployee(); if (employee != null) { return employee.getName(); } return name; } else if ("user.position_id".equalsIgnoreCase(variantName)) { Position position = getCurrentPosition(); if (position != null) { return position.getId(); } return null; } else if ("user.position_level_field".equalsIgnoreCase(variantName)) { Position position = getCurrentPosition(); if (position != null) { return position.getLevelField(); } return null; } return null; } public Position getCurrentPosition() { IOnlineUser onlineUser = IOnlineUser.getInstance(); ActorTarget actor = actorsRuntime.getCurrent(onlineUser.getCode()); if (actor == null) { return null; } Position position = actor.getPosition(); return position; } public Employee getCurrentEmployee() { IOnlineUser onlineUser = IOnlineUser.getInstance(); ActorTarget actor = actorsRuntime.getCurrent(onlineUser.getCode()); if (actor == null) { return null; } Position position = actor.getPosition(); if (employees.isEmpty()) { return null; } Employee employee = employees.getEmployeeByPositionId(position.getId()); return employee; } public static EntitySet calculateUser(String actorTypeCode, String actorId, String id) throws Exception { ActorType actorType = ActorType.parse(actorTypeCode); DataObject dataObject; if (ActorType.User == actorType) { dataObject = DataObject.getInstance("actor_target_user"); EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_user.id", id)); return entitySet; } else if (ActorType.OrgAccount == actorType) { dataObject = DataObject.getInstance("md_org_account_user"); EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("md_org_account.id", id)); return entitySet; } else if (ActorType.Position == actorType) { String positionId = id; if ("Actor-Sales-Leader".equalsIgnoreCase(actorId)) { dataObject = DataObject.getInstance("md_position_hierarchy"); Entity entity = dataObject.getTableEntity(new Filter("position_id", id)); positionId = entity.getString("level2"); } else if ("Actor-Sales-Direction".equalsIgnoreCase(actorId)) { dataObject = DataObject.getInstance("md_position_hierarchy"); Entity entity = dataObject.getTableEntity(new Filter("position_id", id)); positionId = entity.getString("level1"); } dataObject = DataObject.getInstance("md_position_user"); EntitySet positionSet = dataObject.getBrowseEntitySet(new Filter("md_position_employee.position_id", positionId)); return positionSet; } else if(ActorType.Employee == actorType) { // 运营, 商务, 总经理, 财务..... dataObject = DataObject.getInstance("actor_target_position"); EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_right_actor_target.actor_id", actorId)); return entitySet; } return null; } @Override public void writeJSON(IJSONWriter writer) { writer.beginObject(); writeJSONBody(writer); writer.endObject(); } protected void writeJSONBody(IJSONWriter writer) { //1. body writer.write("id", id); writer.write("name", name); writer.write("pass_need_change", passNeedChange); //2. employee if (employees != null) { employees.writeJSON(writer); } //3. organization if (org != null) { org.writeJSON(writer); } //4. capacity writer.beginArray("actors"); for (ActorTarget actor: actorsRuntime) { actor.writeJSON(writer); } writer.endArray(); } }