camunda授权的一些简单操作
阅读原文时间:2023年07月08日阅读:2

/**
* 授权操作
*/
public class ZccAuthorizationService {
AuthorizationService authorizationService;
@Before
public void init(){
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("camunda.cfg.xml");
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
authorizationService = processEngine.getAuthorizationService();
System.out.println("####");
System.out.println("authorizationService:"+authorizationService);
}
@Test
public void getIdentityService(){
init();
}

/\*\*  
 \* select distinct RES.\* from ACT\_RU\_AUTHORIZATION RES WHERE RES.USER\_ID\_ in ( ? ) order by RES.ID\_ asc LIMIT ? OFFSET ?  
 \* 也可以向操作用户那样添加一些查询条件  
 \*/  
@Test  
public void createAuthorizationQuery(){  
    List<Authorization> list = authorizationService.createAuthorizationQuery().userIdIn("zcc2").list();  
    for (int i=0;i<list.size();i++){  
        Authorization authorization = list.get(i);  
        System.out.println(authorization.getId());  
        System.out.println(authorization.getAuthorizationType());  
        System.out.println(authorization.getResourceId());  
        System.out.println(authorization.getGroupId());  
    }

}

public void createAuthorization(String userId, String groupId, Resource resource, String resourceId, Permission\[\] permissions){  
    Authorization authorization=authorizationService.createNewAuthorization(Authorization.AUTH\_TYPE\_GRANT);  
    authorization.setUserId(userId);  
    authorization.setGroupId(groupId);  
    authorization.setResource(resource);  
    authorization.setResourceId(resourceId);  
    authorization.setPermissions(permissions);  
    authorizationService.saveAuthorization(authorization);  
}

public static class TestResource implements  Resource{  
    String reaourceName;int resrouceType;  
    public TestResource(String reaourceName,int resrouceType) {  
       this.reaourceName=reaourceName;  
       this.resrouceType=resrouceType;

    }

    public String getReaourceName() {  
        return reaourceName;  
    }

    public void setReaourceName(String reaourceName) {  
        this.reaourceName = reaourceName;  
    }

    public int getResrouceType() {  
        return resrouceType;  
    }

    public void setResrouceType(int resrouceType) {  
        this.resrouceType = resrouceType;  
    }

    @Override  
    public String resourceName() {  
        return null;  
    }

    @Override  
    public int resourceType() {  
        return 0;  
    }  
}

/\*\*  
 \* Preparing: insert into ACT\_RU\_AUTHORIZATION ( ID\_, TYPE\_, GROUP\_ID\_, USER\_ID\_, RESOURCE\_TYPE\_, RESOURCE\_ID\_, PERMS\_, REV\_ ) values ( ?, ?, ?, ?, ?, ?, ?, 1 )  
 \*  Parameters: 601(String), 1(Integer), null, zcc2(String), 0(Integer), 60(String), 2147483647(Integer)  
 \*/  
@Test  
public void addAuthorization(){  
    Resource resource1=new TestResource("resource1",100);  
    Resource resource2=new TestResource("resource2",200);  
    createAuthorization("zcc2",null,resource1,"60", new Permission\[\]{Permissions.ALL});  
}

@Test  
public void addAuthorization2(){  
    Authorization authorization=authorizationService.createNewAuthorization(Authorization.AUTH\_TYPE\_GRANT);  
    authorization.setUserId("zcc3");  
    authorization.setResource(Resources.APPLICATION);  
    authorization.setResourceType(10);  
    authorization.setResourceId("\*");  
    authorization.setPermissions(new Permission\[\]{Permissions.ALL});  
    authorizationService.saveAuthorization(authorization);

}

/\*\*  
 \* 授权访问用户  
 \*/  
@Test  
public void addAuthorization3(){  
    Authorization authorization=authorizationService.createNewAuthorization(Authorization.AUTH\_TYPE\_GRANT);  
    authorization.setUserId("zcc3");  
    authorization.setResource(Resources.USER);  
    authorization.setResource(Resources.APPLICATION);  
    authorization.setResourceType(1);  
    authorization.setResourceId("\*");  
    authorization.setPermissions(new Permission\[\]{Permissions.CREATE,Permissions.UPDATE});  
    authorizationService.saveAuthorization(authorization);

}

/\*\*  
 \* 授权访问组  
 \*/  
@Test  
public void addAuthorization4(){  
    Authorization authorization=authorizationService.createNewAuthorization(Authorization.AUTH\_TYPE\_GRANT);  
    authorization.setUserId("zcc3");  
    authorization.setResource(Resources.GROUP);  
    authorization.setResourceId("\*");  
    authorization.setPermissions(new Permission\[\]{Permissions.READ,Permissions.CREATE,Permissions.UPDATE});  
    authorizationService.saveAuthorization(authorization);

}

/\*\*  
 \* 授权访问租户  
 \*/  
@Test  
public void addAuthorization5(){  
    Authorization authorization=authorizationService.createNewAuthorization(Authorization.AUTH\_TYPE\_GRANT);  
    authorization.setUserId("zcc3");  
    authorization.setResource(Resources.TENANT);  
    authorization.setResourceId("\*");  
    authorization.setPermissions(new Permission\[\]{Permissions.READ,Permissions.CREATE,Permissions.UPDATE});  
    authorizationService.saveAuthorization(authorization);

}

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章