|
(→AHPSCRIPTS-78)
|
(→AHPSCRIPTS-77)
|
| Line 431: | |||
| //Close unit of work | //Close unit of work | ||
| uow.close(); </pre> | uow.close(); </pre> | ||
| + | = Assign Read, Write, Security Permissions to All Codestation Projects For Role = | ||
| + | This script takes the usual arguments server port username password then it takes role and a string of up to 3 characters being rws read write security. Then for every CS project in the server it gives those permissions to the provided role. | ||
| + | ==== AHPSCRIPTS-76 ==== | ||
| + | <pre>import com.urbancode.anthill3.main.client.AnthillClient; | ||
| + | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
| + | import com.urbancode.codestation2.domain.project.*; | ||
| + | import com.urbancode.anthill3.domain.security.*; | ||
| + | final String READ = "read"; | ||
| + | final String WRITE = "write"; | ||
| + | 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 permissions = bsh.args[5]; | ||
| + | boolean settingRead = false; | ||
| + | boolean settingWrite = false; | ||
| + | boolean settingSecurity = false; | ||
| + | if (permissions.contains("r")) { | ||
| + | print("Setting Read"); | ||
| + | settingRead = true; | ||
| + | } | ||
| + | if (permissions.contains("w")) { | ||
| + | print("Setting Write"); | ||
| + | settingWrite = 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 | ||
| + | CodestationProject[] projects = CodestationProjectFactory.getInstance().restoreAllCodestation(); | ||
| + | if (projects == null || projects.length == 0) { | ||
| + | throw new Exception("No Projects found."); | ||
| + | } | ||
| + | for (CodestationProject project : projects) { | ||
| + | boolean hasRead = false; | ||
| + | boolean hasWrite = false; | ||
| + | boolean hasSecurity = false; | ||
| + | boolean isDirty = false; | ||
| + | print("\tSetting permissions for Project:" + project.getName()); | ||
| + | Resource resource = ResourceFactory.getInstance().restoreForPersistent(project); | ||
| + | if (resource == null) { | ||
| + | throw new Exception("Project Resource not found. Please contact support."); | ||
| + | } | ||
| + | Permission[] permissions = PermissionFactory.getInstance().restoreAllForResource(resource); | ||
| + | for (Permission permission : permissions) { | ||
| + | if (permission.getRole().equals(role)) { | ||
| + | if (settingRead && READ.equals(permission.getAction())) { | ||
| + | print("\t\tHas Read"); | ||
| + | hasRead = true; | ||
| + | } | ||
| + | else if (settingWrite && WRITE.equals(permission.getAction())) { | ||
| + | print("\t\tHas Write"); | ||
| + | hasWrite = true; | ||
| + | } | ||
| + | else if (settingSecurity && SECURITY.equals(permission.getAction())) { | ||
| + | print("\t\tHas Security"); | ||
| + | hasSecurity = true; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if (settingRead && !hasRead) { | ||
| + | print("\tSetting Read Permission"); | ||
| + | Permission read = new Permission(resource, READ, role); | ||
| + | read.store(); | ||
| + | isDirty = true; | ||
| + | } | ||
| + | if (settingWrite && !hasWrite) { | ||
| + | print("\tSetting Write Permission"); | ||
| + | Permission write = new Permission(resource, WRITE, role); | ||
| + | write.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(); | ||
| + | } | ||
| + | } | ||
| + | //Close unit of work | ||
| + | uow.close(); </pre> | ||