Version Differences for Remoting Specific Scripts

(AHPSCRIPTS-14)
(AHPSCRIPTS-67)
Line 187:
       
  uow.commit();</pre>    uow.commit();</pre> 
       
    + = Set Role Permissions For Workflow of Given Name in Every Project =  
    + This script takes the usual server port username password as well as a role name a workflow name and a string of up to 2 characters being xs for execute and security. This script will go through every project and if that project contains a workflow with the given name it will assign the requested permissions to the entered in role.  
    + ==== AHPSCRIPTS-78 ====  
    + <pre>import com.urbancode.anthill3.main.client.AnthillClient;  
    + import com.urbancode.anthill3.persistence.UnitOfWork;  
    + import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.workflow.*;  
    + import com.urbancode.anthill3.domain.security.*;  
       
    + final String EXECUTE = "execute";  
    + final String SECURITY = "security";  
       
    + String serverHost = "localhost";  
    + int serverPort = 4567;  
    + String userName = "admin";  
    + String password = "admin";  
       
    + serverHost = bsh.args[0];  
    + serverPort = Integer.parseInt(bsh.args[1]);  
    + userName = bsh.args[2];  
    + password = bsh.args[3];  
       
    + if (serverHost == null || serverHost.length() == 0) {  
    + throw new IllegalArgumentException("Must Specify a Server Host");  
    + }  
       
    + String roleName = bsh.args[4];  
    + print("Role:" + roleName);  
       
    + String workflowName = bsh.args[5];  
    + print("Workflow:" + workflowName);  
       
    + String permissions = bsh.args[6];  
    + boolean settingExecute = false;  
    + boolean settingSecurity = false;  
       
    + if (permissions.contains("x")) {  
    + print("Setting Execute");  
    + settingExecute = true;  
    + }  
    + if (permissions.contains("s")) {  
    + print("Setting Security");  
    + settingSecurity = true;  
    + }  
       
    + // obtain connection to the Anthill server  
    + AnthillClient anthill = AnthillClient.connect(serverHost, serverPort,  
    + userName, password);  
       
    + // create a Unit of Work  
    + UnitOfWork uow = anthill.createUnitOfWork();  
       
    + Role role = RoleFactory.getInstance().restoreForName(roleName);  
    + if (role == null) {  
    + throw new Exception("Role " + roleName + " not found.");  
    + }  
       
    + // Project  
    + Project[] projects = ProjectFactory.getInstance().restoreAllActive();  
    + if (projects == null || projects.length == 0) {  
    + throw new IllegalStateException("No Projects found.");  
    + }  
       
    + for (Project project : projects) {  
    + boolean hasExecute = false;  
    + boolean hasSecurity = false;  
    + boolean isDirty = false;  
       
    + print("\tSetting permissions for Project:" + project.getName());  
       
    + Workflow workflow = project.getWorkflow(workflowName);  
    + if (workflow != null) {  
    + Resource resource = ResourceFactory.getInstance().restoreForPersistent(workflow);  
    + if (resource == null) {  
    + throw new Exception("Workflow Resource not found. Please contact support.");  
    + }  
       
    + Permission[] permissions = PermissionFactory.getInstance().restoreAllForResource(resource);  
    + for (Permission permission : permissions) {  
    + if (permission.getRole().equals(role)) {  
    + if (settingExecute && EXECUTE.equals(permission.getAction())) {  
    + print("\t\tHas Execute");  
    + hasExecute = true;  
    + }  
    + else if (settingSecurity && SECURITY.equals(permission.getAction())) {  
    + print("\t\tHas Security");  
    + hasSecurity = true;  
    + }  
    + }  
    + }  
       
    + if (settingExecute && !hasExecute) {  
    + print("\tSetting Execute Permission");  
    + Permission execute = new Permission(resource, EXECUTE, role);  
    + execute.store();  
    + isDirty = true;  
    + }  
    + if (settingSecurity && !hasSecurity) {  
    + print("\tSetting Security Permission");  
    + Permission security = new Permission(resource, SECURITY, role);  
    + security.store();  
    + isDirty = true;  
    + }  
       
    + // commit to database  
    + if (isDirty) {  
    + uow.commit();  
    + }  
    + }  
    + else {  
    + print("Workflow " + workflowName + " not found for Project " + project.getName());  
    + }  
       
    + }  
       
    + //Close unit of work  
    + uow.close(); </pre>