(→AHPSCRIPTS-67)
|
(→AHPSCRIPTS-78)
|
Line 305: | |||
//Close unit of work | //Close unit of work | ||
uow.close(); </pre> | uow.close(); </pre> | ||
+ | = Set Role Permissions For All Anthill Projects = | ||
+ | This script will take the usual inputs server port username password as well as role name and a script of up to 3 characters being rws. It will then set those given permissions read write security for the given role on all Anthill3 Projects in the system. | ||
+ | ==== AHPSCRIPTS-77 ==== | ||
+ | <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.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 | ||
+ | Project[] projects = ProjectFactory.getInstance().restoreAllActive(); | ||
+ | if (projects == null || projects.length == 0) { | ||
+ | throw new Exception("No Projects found."); | ||
+ | } | ||
+ | for (Project 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> |