Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base.jee.servlet;
  
 import base.KeyValue;
 
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
 public class GroupRolesServlet extends BaseServlet {
 
 	private static final long serialVersionUID = 1L;
 
 	public GroupRolesServlet(JeeBase jeethrows IOException {
 		super(jee);
 	}
 
 	@SuppressWarnings("unchecked")
 	public void doGet(String tokenUser uHttpServletRequest requestHttpServletResponse responsethrows ServletExceptionIOException {
 
 		UUID uuid = UUID.fromString(request.getParameter("uuid"));
 		String error = null;
 
 		String a = request.getParameter("a");
 		if(a != null && !a.equals(u.getFormAuthToken())) {
 			throw new IllegalArgumentException("Invalid authentication token.");
 		}
 
 		if(request.getParameter("add_role") != null && request.getParameter("role_resource") != null) {
 			if(a == null) {
 				throw new IllegalArgumentException("Invalid authentication token.");
 			}
 					request.getParameter("add_role"),
 					.getRoles().getRole(request.getParameter("add_role")).getResourceType(),
 					request.getParameter("role_resource"));
 			response.setHeader("Location".get("base.url""") + "/group.roles?uuid=" + uuid + "&saved=ok");
 			response.setStatus(302);
 		}
 
 		if(request.getParameter("delete_role") != null && request.getParameter("resource") != null) {
 			if(a == null) {
 				throw new IllegalArgumentException("Invalid authentication token.");
 			}
 					.getRoles().getRole(request.getParameter("delete_role")).getResourceType(),
 					request.getParameter("resource"),
 					uuid,
 					request.getParameter("delete_role")
 					);
 			response.setHeader("Location".get("base.url""") + "/group.roles?uuid=" + uuid + "&saved=ok");
 			response.setStatus(302);
 		}
 
 		if(request.getParameter("admin_roles") != null) {
 			if(a == null) {
 				throw new IllegalArgumentException("Invalid authentication token.");
 			}
 
 			// Update the (non resource based) roles.
 				if(request.getParameter("role_" + r.getRole()) != null && request.getParameter("role_" + r.getRole()).equals("true")) {
 					.getAPI().upsertGroupRole(uuuidr.getRole());
 				} else {
 					.getAPI().deleteGroupRole(uuuidr.getRole());
 				}
 			}
 
 			response.setHeader("Location".get("base.url""") + "/group.roles?uuid=" + uuid + "&saved=ok");
 			response.setStatus(302);
 		}
 
 		Group g = .getAPI().getGroup(uuuid).get(0);
 
 		List<base.security.PersonRolecurrentRoles = .getAPI().getGroupRoles(uuuid);
 
 		String roleToAdd = null;
 		String roleToAddName = null;
 		List<KeyValuepossibleResourceBasedRoles = null;
 		if(request.getParameter("add_role") != null && request.getParameter("role_resource") == null) {
			roleToAdd = request.getParameter("add_role");
			Role role = .getRoles().getRole(roleToAdd);
			roleToAddName = role.getName();
			possibleResourceBasedRoles = .getRoles().getResourceLookup(role.getResourceType()).list();
		}
		ST page = .getPage(u"group_roles");
		page.add("uuid"g.getUuid());
		// Used in the person editing form
		page.add("g"g);
		List<Map<StringObject>> adminRoles = buildNonResourceBasedRoleList(uuuidcurrentRolesrequest);
		page.add("roles"adminRoles);
		page.add("has_custom_role"adminRoles.size() > 0 && !(((Role)adminRoles.get(0).get("role")).isInternal()));
		page.add("error"error);
		// Used in the role management form
			page.add("role_manager"false);
else {
			page.add("role_manager"true);
			page.add("possible_resource_based_roles"possibleResourceBasedRoles);
			page.add("role_to_add"roleToAdd);
			page.add("role_to_add_name"roleToAddName);
			page.add("current_resource_based_roles"buildResourceRoleList(currentRoles));
		}
		if(request.getParameter("saved") != null) {
			page.add("success""<p>Saved changes to " + StringHelper.escapeHtml(g.getName()) + " roles.</p>");
else {
			page.add("success"null);
		}
		page.add("ldap_enabled".getSettings().get("ldap.enabled""false").equals("true"));
		page.add("breadcrumbs"new Breadcrumb("People""/people"));
		page.add("breadcrumbs"new Breadcrumb("Groups""/groups"));
		page.add("breadcrumbs"new Breadcrumb(g.getName() + " Roles"null));
		page.add("auth"u.getFormAuthToken());
		response.getWriter().write(page.render());
	}

User roles filtered by ones that are resource restricted.
		List<Map<StringString>> currentResourceBasedRoles = new LinkedList<>();
		for(base.security.PersonRole r : currentRoles) {
			if(r.getUid() != null) {
				Map<StringStringinfo = new Hashtable<>();
				info.put("role"r.getRole());
				info.put("uid"r.getUid());
				Role role = .getRoles().getRole(r.getRole());
				if(role == null) {
					continue;
				}
				info.put("name"role.getName());
				if(rs != null) {
					info.put("resource"rs.getValue());
else {
					info.put("resource""Unknown/missing record: " + r.getUid());
				}
				currentResourceBasedRoles.add(info);
			}
		}
		return currentResourceBasedRoles;
	}

Build the list of available roles, associated with a "selected" indicator for this person
	public static List<Map<StringObject>> buildNonResourceBasedRoleList(JeeBase jeeUser userUUID uuidList<base.security.PersonRolecurerntRolesHttpServletRequest requestthrows IOException {
		List<Map<StringObject>> roles = new LinkedList<>();
		//TODO: What is the use rand uuid used for?!?
		for(Role role : jee.getRoles().) {
			Map<StringObjecti = new Hashtable<>();
			i.put("role"role);
			if(request.getParameter("first_name") != null) {
				i.put("selected"request.getParameter("role_" + role.getRole()) != null && request.getParameter("role_" + role.getRole()).equals("true"));
else {
				for(base.security.PersonRole rl : curerntRoles) {
					if(rl.getRole().equals(role.getRole())) {
						i.put("selected"true);
						break;
					}
				}
			}
			if(!i.containsKey("selected")) {
				i.put("selected"false);
			}
			roles.add(i);
		}
		return roles;
	}
New to GrepCode? Check out our FAQ X