@Override public String getDisplayName() { String name = Messages.RoleBasedAuthorizationStrategy_DisplayName(); if (!RoleBasedAuthorizationStrategy.isCreateAllowed()) name += " (<font color=\"red\">(Require >1.565 core)</font>"; return name; }
@Override public void checkName(String name) throws Failure { boolean matches = false; ArrayList<String> badList = null; AuthorizationStrategy auth = Jenkins.getInstance().getAuthorizationStrategy(); if (auth instanceof RoleBasedAuthorizationStrategy) { RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) auth; // firstly check global role SortedMap<Role, Set<String>> gRole = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.GLOBAL); for (SortedMap.Entry<Role, Set<String>> entry : gRole.entrySet()) { if (entry.getKey().hasPermission(Item.CREATE)) return; } // check project role with pattern SortedMap<Role, Set<String>> roles = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.PROJECT); badList = new ArrayList<String>(roles.size()); for (SortedMap.Entry<Role, Set<String>> entry : roles.entrySet()) { Role key = entry.getKey(); if (key.hasPermission(Item.CREATE)) { String namePattern = key.getPattern().toString(); if (StringUtils.isNotBlank(namePattern) && StringUtils.isNotBlank(name)) { if (Pattern.matches(namePattern, name)) { matches = true; } else { badList.add(namePattern); } } } } } if (!matches) { String error; if (badList != null && !badList.isEmpty()) // TODO beatify long outputs? error = jenkins.model.Messages.Hudson_JobNameConventionNotApplyed(name, badList.toString()); else error = Messages.RoleBasedProjectNamingStrategy_NoPermissions(); throw new Failure(error); } }