KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > user > DefaultPermissionManager


1 /* Compent
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.user;
26
27 import org.snipsnap.snip.Ownable;
28 import org.snipsnap.snip.Snip;
29
30 import java.util.*;
31
32 /**
33  * Manages security and checks if a role has a permission.
34  *
35  * @author Stephan J. Schmidt
36  * @version $Id: DefaultPermissionManager.java 1257 2003-12-11 13:36:55Z leo $
37  */

38
39 public class DefaultPermissionManager implements PermissionManager {
40   private AuthenticationService authenticationService;
41
42   private Permission[] EDITOR_PERMISSIONS = {
43     Permission.REMOVE_SNIP, Permission.EDIT_COMMENT, Permission.POST_TO_SNIP, Permission.LOCK_SNIP };
44   private Permission[] USER_PERMISSIONS = {
45     Permission.EDIT_SNIP, Permission.CREATE_SNIP, Permission.POST_COMMENT };
46   private Permission[] OWNER_PERMISSIONS = {
47     Permission.EDIT_COMMENT, Permission.LOCK_SNIP };
48   private Permission[] GUEST_PERMISSIONS = {
49     Permission.VIEW_SNIP };
50
51   private Map rolesToPermissions;
52   private Map permissionToRoles;
53
54   public DefaultPermissionManager(AuthenticationService authenticationService) {
55     this.authenticationService = authenticationService;
56
57     rolesToPermissions = new HashMap();
58     rolesToPermissions.put("Editor", EDITOR_PERMISSIONS);
59     rolesToPermissions.put("User", USER_PERMISSIONS);
60     rolesToPermissions.put("Owner", OWNER_PERMISSIONS);
61     rolesToPermissions.put("Guest", GUEST_PERMISSIONS);
62
63     permissionToRoles = new HashMap();
64     Iterator iterator = rolesToPermissions.keySet().iterator();
65     while (iterator.hasNext()) {
66       String JavaDoc role = (String JavaDoc) iterator.next();
67       Permission[] rolePermissions = (Permission[]) rolesToPermissions.get(role);
68       for (int i = 0; i < rolePermissions.length; i++) {
69         Permission permission = rolePermissions[i];
70         Set permissions;
71         if (permissionToRoles.containsKey(permission)) {
72           permissions = (Set) permissionToRoles.get(permission);
73         } else {
74           permissions = new HashSet();
75           permissionToRoles.put(permission, permissions);
76         }
77         permissions.add(role);
78       }
79     }
80     //System.err.println("rolesToPermissions="+rolesToPermissions);
81
//System.err.println("permissionToRoles="+permissionToRoles);
82
}
83
84   public boolean check(Permission permission, User user, Snip snip) {
85     // for all roles of the user
86
// check for all permission of the roles
87
// if permission is in
88
Set roles = getRoles(user, snip).getRoleSet();
89     if (! permissionToRoles.containsKey(permission)) {
90       return false;
91     } else {
92       //System.err.println("Permission found.");
93
Set rolesWithPermission = (Set) permissionToRoles.get(permission);
94       //System.err.println("Roles="+roles);
95
//System.err.println("rolesWithP="+rolesWithPermission);
96
rolesWithPermission.retainAll(roles);
97       return ! rolesWithPermission.isEmpty();
98     }
99   }
100
101   /**
102    * Return the roles for a user
103    *
104    * @param user
105    * @return
106    */

107   private Roles getRoles(User user) {
108     Roles userRoles = new Roles(user.getRoles());
109     if (authenticationService.isAuthenticated(user)) {
110       userRoles.add(Roles.AUTHENTICATED);
111     }
112     return userRoles;
113   }
114
115   /**
116    * Return the roles for a user in a snip
117    * context. This adds the owner role to roles
118    * list
119    *
120    * @param user User to check
121    * @param object Object with possible owner
122    * @return List of roles for user and object
123    */

124   private Roles getRoles(User user, Snip object) {
125     Roles roles = getRoles(user);
126     if (object instanceof Ownable) {
127       Ownable o = object;
128       if (o.isOwner(user)) {
129         roles.add(Roles.OWNER);
130       }
131     }
132     return roles;
133   }
134 }
135
136
Popular Tags