1 package org.tigris.scarab.actions.admin; 2 3 48 49 50 52 import org.apache.turbine.TemplateContext; 54 import org.apache.turbine.RunData; 55 import org.apache.turbine.tool.IntakeTool; 56 import org.apache.fulcrum.intake.model.Group; 57 import org.apache.fulcrum.security.TurbineSecurity; 58 import org.apache.fulcrum.security.entity.Role; 59 import org.apache.fulcrum.security.entity.Permission; 60 import org.apache.fulcrum.security.util.EntityExistsException; 61 import org.apache.fulcrum.security.util.PermissionSet; 62 63 import org.tigris.scarab.om.ScarabUser; 65 import org.tigris.scarab.tools.ScarabLocalizationTool; 66 import org.tigris.scarab.util.ScarabConstants; 67 import org.tigris.scarab.actions.base.RequireLoginFirstAction; 68 69 76 public class ManageRoles extends RequireLoginFirstAction 77 { 78 79 82 public void doGotoaddrole(RunData data, TemplateContext context) 83 throws Exception 84 { 85 setTarget(data, "admin,AddRole.vm"); 86 } 87 88 91 public void doGotoeditrole(RunData data, TemplateContext context) 92 throws Exception 93 { 94 checkParamValidity(data, context, "admin,EditRole.vm"); 95 } 96 97 100 public void doGotodeleterole(RunData data, TemplateContext context) 101 throws Exception 102 { 103 checkParamValidity(data, context, "admin,DeleteRole.vm"); 104 } 105 106 109 public void doAddrole(RunData data, TemplateContext context) 110 throws Exception 111 { 112 IntakeTool intake = getIntakeTool(context); 113 ScarabLocalizationTool l10n = getLocalizationTool(context); 114 115 if (intake.isAllValid()) 116 { 117 Object user = data.getUser().getTemp(ScarabConstants.SESSION_REGISTER); 118 119 Group editRole = null; 120 if (user != null && user instanceof ScarabUser) 121 { 122 editRole = intake.get("EditRole", ((ScarabUser)user).getQueryKey(), false); 123 } 124 else 125 { 126 editRole = intake.get("EditRole", IntakeTool.DEFAULT_KEY, false); 127 } 128 String name = editRole.get("RoleName").toString(); 129 130 try 131 { 132 Role role = TurbineSecurity.getNewRole(null); 133 role.setName(name); 134 135 TurbineSecurity.addRole(role); 136 137 String msg = l10n.format("RoleCreated", name); 138 getScarabRequestTool(context).setConfirmMessage(msg); 139 140 data.getParameters().setString("name", name); 141 doGotoeditrole(data, context); 142 } 143 catch (EntityExistsException eee) 144 { 145 String msg = l10n.format("RoleExists", name); 146 getScarabRequestTool(context).setConfirmMessage(msg); 147 } 148 } 149 } 150 151 154 public void doEditrole(RunData data, TemplateContext context) 155 throws Exception 156 { 157 160 String name = data.getParameters().getString("name"); 161 checkParamValidity(data, context, null); 162 Role role = TurbineSecurity.getRole(name); 163 164 168 PermissionSet rolePermissions = role.getPermissions(); 169 170 173 Permission[] permissions = TurbineSecurity.getAllPermissions() 174 .getPermissionsArray(); 175 176 String roleName = role.getName(); 177 178 for (int i = 0; i < permissions.length; i++) 179 { 180 String permissionName = permissions[i].getName(); 181 String rolePermission = roleName + permissionName; 182 183 String formRolePermission = data.getParameters().getString(rolePermission); 184 Permission permission = TurbineSecurity.getPermission(permissionName); 185 186 187 if (formRolePermission != null && !rolePermissions.contains(permission)) 188 { 189 194 195 role.grant(permission); 196 } 197 else if (formRolePermission == null && rolePermissions.contains(permission)) 198 { 199 204 role.revoke(permission); 205 } 206 } 207 } 208 209 213 public void doDeleterole(RunData data, TemplateContext context) 214 throws Exception 215 { 216 219 String name = data.getParameters().getString("name"); 220 Role role = TurbineSecurity.getRole(name); 221 TurbineSecurity.removeRole(role); 222 223 ScarabLocalizationTool l10n = getLocalizationTool(context); 224 225 String msg = l10n.format("RoleDeleted", name); 226 getScarabRequestTool(context).setConfirmMessage(msg); 227 setTarget(data, data.getParameters() 228 .getString(ScarabConstants.NEXT_TEMPLATE, "admin,ManageRoles.vm")); 229 } 230 231 232 235 public void doCancel(RunData data, TemplateContext context) throws Exception 236 { 237 setTarget(data, data.getParameters() 238 .getString(ScarabConstants.CANCEL_TEMPLATE, "admin,AdminIndex.vm")); 239 } 240 241 244 public void doPerform(RunData data, TemplateContext context) 245 throws Exception 246 { 247 doCancel(data,context); 248 } 249 250 257 protected void checkParamValidity(RunData data, TemplateContext context, 258 String target) 259 { 260 String name = data.getParameters().getString("name"); 261 262 if (name == null || name.length() == 0) 263 { 264 ScarabLocalizationTool l10n = getLocalizationTool(context); 265 String msg = l10n.get("NoRoleSelected"); 266 getScarabRequestTool(context).setConfirmMessage(msg); 267 setTarget(data, "admin,ManageRoles.vm"); 268 } 269 else 270 { 271 if (target != null) 272 { 273 setTarget(data, target); 274 } 275 } 276 } 277 } 278 | Popular Tags |