Search in sources :

Example 6 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class DepartmentController method deptTreeGet.

@GetMapping("dept-tree")
public JSON deptTreeGet() {
    JSONArray dtree = new JSONArray();
    Department[] ds = Application.getUserStore().getTopDepartments();
    sortByName(ds);
    for (Department root : ds) {
        dtree.add(recursiveDeptTree(root));
    }
    return dtree;
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) JSONArray(com.alibaba.fastjson.JSONArray) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 7 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class RoleBaseQueryFilter method evaluate.

@Override
public String evaluate(final Entity entity) {
    if (user == null || !user.isActive()) {
        return DENIED.evaluate(null);
    } else if (user.isAdmin()) {
        return ALLOWED.evaluate(null);
    }
    Entity useMainEntity = null;
    if (!MetadataHelper.hasPrivilegesField(entity)) {
        // NOTE BIZZ 实体全部用户可见
        if (MetadataHelper.isBizzEntity(entity) || EasyMetaFactory.valueOf(entity).isPlainEntity()) {
            return ALLOWED.evaluate(null);
        } else if (entity.getMainEntity() != null) {
            useMainEntity = entity.getMainEntity();
        } else {
            log.warn("None privileges entity use `Application#createQueryNoFilter` please : {} \n\t{}", entity, StringUtils.join(Thread.currentThread().getStackTrace(), "\n\t"));
            return DENIED.evaluate(null);
        }
    }
    // 未配置权限的默认拒绝
    // 明细实体使用主实体权限
    final Privileges ep = user.getOwningRole().getPrivileges(useMainEntity != null ? useMainEntity.getEntityCode() : entity.getEntityCode());
    if (ep == Privileges.NONE) {
        return DENIED.evaluate(null);
    }
    String owningFormat = "%s = '%s'";
    Field dtmField = null;
    if (useMainEntity != null) {
        dtmField = MetadataHelper.getDetailToMainField(entity);
        owningFormat = dtmField.getName() + "." + owningFormat;
    }
    final String customFilter = buildCustomFilter(ep, dtmField);
    final String shareFilter = buildShareFilter(entity, dtmField);
    final DepthEntry depth = ep.superlative(useAction);
    if (depth == BizzDepthEntry.GLOBAL) {
        if (customFilter == null) {
            return ALLOWED.evaluate(null);
        } else {
            return String.format("(%s or %s)", customFilter, shareFilter);
        }
    }
    if (depth == BizzDepthEntry.PRIVATE) {
        String baseFilter = String.format(owningFormat, EntityHelper.OwningUser, user.getIdentity());
        return joinFilters(baseFilter, customFilter, shareFilter);
    }
    // 部门
    Department dept = user.getOwningDept();
    String deptFilter = String.format(owningFormat, EntityHelper.OwningDept, dept.getIdentity());
    if (depth == BizzDepthEntry.LOCAL) {
        return joinFilters(deptFilter, customFilter, shareFilter);
    } else if (depth == BizzDepthEntry.DEEPDOWN) {
        Set<String> set = new HashSet<>();
        set.add(deptFilter);
        for (BusinessUnit ch : dept.getAllChildren()) {
            set.add(String.format(owningFormat, EntityHelper.OwningDept, ch.getIdentity()));
        }
        deptFilter = StringUtils.join(set, " or ");
        return joinFilters(deptFilter, customFilter, shareFilter);
    }
    return DENIED.evaluate(null);
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) Department(com.rebuild.core.privileges.bizz.Department) HashSet(java.util.HashSet) Set(java.util.Set) BusinessUnit(cn.devezhao.bizz.security.member.BusinessUnit) CustomEntityPrivileges(com.rebuild.core.privileges.bizz.CustomEntityPrivileges) Privileges(cn.devezhao.bizz.privileges.Privileges) DepthEntry(cn.devezhao.bizz.privileges.DepthEntry) BizzDepthEntry(cn.devezhao.bizz.privileges.impl.BizzDepthEntry)

Example 8 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class UserController method deleteChecks.

@RequestMapping("delete-checks")
public JSON deleteChecks(@IdParam ID bizzId) {
    int hasMember = 0;
    int hasChild = 0;
    if (bizzId.getEntityCode() == EntityHelper.Department) {
        Department dept = Application.getUserStore().getDepartment(bizzId);
        hasMember = dept.getMembers().size();
        hasChild = dept.getChildren().size();
    } else if (bizzId.getEntityCode() == EntityHelper.Role) {
        hasMember = UserHelper.getMembers(bizzId).length;
        hasMember += UserHelper.getRoleMembers(bizzId).size();
    } else if (bizzId.getEntityCode() == EntityHelper.User) {
        // NOTE 仅检查是否登陆过。严谨些还应该检查是否有其他业务数据
        Object[] hasLogin = Application.createQueryNoFilter("select count(logId) from LoginLog where user = ?").setParameter(1, bizzId).unique();
        hasMember = ObjectUtils.toInt(hasLogin[0]);
    }
    return JSONUtils.toJSONObject(new String[] { "hasMember", "hasChild" }, new Object[] { hasMember, hasChild });
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) JSONObject(com.alibaba.fastjson.JSONObject)

Example 9 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class DepartmentController method recursiveDeptTree.

private JSONObject recursiveDeptTree(Department parent) {
    JSONObject parentJson = new JSONObject();
    parentJson.put("id", parent.getIdentity());
    parentJson.put("name", parent.getName());
    parentJson.put("disabled", parent.isDisabled());
    JSONArray children = new JSONArray();
    BusinessUnit[] ds = parent.getChildren().toArray(new BusinessUnit[0]);
    sortByName(ds);
    for (BusinessUnit child : ds) {
        children.add(recursiveDeptTree((Department) child));
    }
    if (!children.isEmpty()) {
        parentJson.put("children", children);
    }
    return parentJson;
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) JSONObject(com.alibaba.fastjson.JSONObject) BusinessUnit(cn.devezhao.bizz.security.member.BusinessUnit) JSONArray(com.alibaba.fastjson.JSONArray)

Example 10 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class UserHelperTest method test.

@Test
public void test() {
    UserHelper.getDepartment(UserService.ADMIN_USER);
    UserHelper.getName(DepartmentService.ROOT_DEPT);
    UserHelper.isActive(UserService.SYSTEM_USER);
    UserHelper.isAdmin(SIMPLE_USER);
    UserHelper.getMembers(RoleService.ADMIN_ROLE);
    UserHelper.getMembers(DepartmentService.ROOT_DEPT);
    Department dept = Application.getUserStore().getDepartment(DepartmentService.ROOT_DEPT);
    UserHelper.getAllChildren(dept);
    Application.getUserStore().getUser(SIMPLE_USER).isActive();
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) Test(org.junit.jupiter.api.Test)

Aggregations

Department (com.rebuild.core.privileges.bizz.Department)12 Field (cn.devezhao.persist4j.Field)4 JSONArray (com.alibaba.fastjson.JSONArray)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ID (cn.devezhao.persist4j.engine.ID)3 BusinessUnit (cn.devezhao.bizz.security.member.BusinessUnit)2 Entity (cn.devezhao.persist4j.Entity)2 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)2 User (com.rebuild.core.privileges.bizz.User)2 Test (org.junit.jupiter.api.Test)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 DepthEntry (cn.devezhao.bizz.privileges.DepthEntry)1 Privileges (cn.devezhao.bizz.privileges.Privileges)1 BizzDepthEntry (cn.devezhao.bizz.privileges.impl.BizzDepthEntry)1 EasyField (com.rebuild.core.metadata.easymeta.EasyField)1 CustomEntityPrivileges (com.rebuild.core.privileges.bizz.CustomEntityPrivileges)1 DataSpecificationException (com.rebuild.core.service.DataSpecificationException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1