Source code: org/acs/damsel/srvr/auth/AuthMgr.java
1 package org.acs.damsel.srvr.auth;
2
3 import java.sql.*;
4 import java.util.*;
5 import org.apache.log4j.*;
6
7 import org.acs.damsel.srvr.user.*;
8 import org.acs.damsel.srvr.db.*;
9
10 /**
11 * <p>Class Name: AuthMgr</p>
12 * <p>Description: The Authorization Manager (AuthMgr for short) is a manager-level
13 * class that facilitates communication between the server layer and the presentation
14 * layer. It provides support for all things related to authorization, including methods
15 * for asking whether a specific user has permission to perform a specific action,
16 * retrieving permission names from the database, authenticating a user based on
17 * username and password, and verifying whether a user is in an administrator.</p>
18 * @version 1.0
19 */
20
21 public class AuthMgr {
22
23 private static Logger log = Logger.getLogger(AssetDB.class);
24
25 public AuthMgr() {
26 super();
27 }
28
29 /**
30 * Method to validate high level permissions associated with a specified user in
31 * the GroupsTable in the database.
32 * @param username String containing userName of user whose permissions are being checked.
33 * @param action String containing action being called. Note, action will ONLY be
34 * Create, Edit, or Remove.
35 * @param target String containing target action will be performed on. Note that target
36 * can only be Collections, Repositories, Assets, Users, Schemas, or Groups
37 * @throws SQLException
38 * @return boolean true if username can perform the action on the target, false otherwise
39 */
40 public boolean can(String username, String action, String target) throws
41 SQLException {
42 String colName = new String();
43 action = (action.substring(0, 1)).toUpperCase() + action.substring(1);
44 target = (target.substring(0, 1)).toUpperCase() + target.substring(1);
45 colName = "Can" + action + target;
46 return DBUtils.instance().canDoHighPerm(username, colName);
47 }
48
49 /**
50 * Method validates low level permissions associated with a specified user in
51 * the PermissionsTable table in the database.
52 * @param username String containing username of user whose permissions are being checked.
53 * @param action String containing action being called. Note that action can ONLY be
54 * Read, Write, or Delete.
55 * @param targetType String containing the table that the object the action will be performed
56 * on is in.
57 * @param keyName String containing the name of the table's key field.
58 * @param keyValue String containing the name of the table's key value.
59 * @throws SQLException
60 * @return boolean
61 * michelle s. and christy
62 */
63 public boolean can(String username, String action, String targetType,
64 String keyName, String keyValue) throws SQLException{
65 return DBUtils.instance().canDoLowPerm(username, action, targetType, keyName, keyValue);
66 }
67
68 /**
69 * Method returns a validated user, returns null if user is not valid, or
70 * password for user is incorrect.
71 * @param username String containing the username of the user to get from db
72 * @param password String containing the password of the user to get from the db
73 * @return User containing the User from the db if username and password were valid, null otherwise.
74 */
75 public User getUser(String username, String password) {
76 try {
77 return AssetDB.instance().getUser(username, password);
78 }
79 catch (SQLException ex) {
80 return null;
81 }
82 }
83
84 /**
85 * Determines if the specified user is an administrator.
86 * @param user User representing the user in question
87 * @return true if the specified user is an administrator, false otherwise
88 */
89 public boolean isAdministrator(User user) {
90 try {
91 Vector users = AssetDB.instance().getUsersInGroup("Administrators");
92 String u;
93 for (int i = 0; i < users.size(); i ++) {
94 u = (String) users.elementAt(i);
95 if (u.equals(user.getUserName()))
96 return true;
97 }
98 }
99 catch (SQLException ex) {
100 log.warn("Unexpected SQLException in AuthMgr.isAdministrator() " + ex.getMessage());
101 }
102 return false;
103 }
104
105 /**
106 * This method takes a vector of fieldNames and a vector of values and returns the
107 * corresponding permissionID. Note that each vector will ALWAYS have ONLY nine elements.
108 * @param fields Vector vector of strings containing all field names, such as OwnerRead,
109 * OwnerWrite, OwnerDelete, GroupRead, etc.
110 * @param values Vector vector of strings containing the value for each field, either
111 * true or false.
112 * @return String, string form of permissionID.
113 */
114 public int getAllPermissions(Vector fields, Vector values){
115 char [] permissionString = { '-','-','-','-','-','-','-','-','-'};
116 if(values.elementAt(fields.indexOf("ownerRead")).equals("true") )
117 permissionString[0] = 'r';
118 if(values.elementAt(fields.indexOf("ownerWrite")).equals("true") )
119 permissionString[1] = 'w';
120 if(values.elementAt(fields.indexOf("ownerDelete")).equals("true") )
121 permissionString[2] = 'd';
122 if(values.elementAt(fields.indexOf("groupRead")).equals("true") )
123 permissionString[3] = 'r';
124 if(values.elementAt(fields.indexOf("groupWrite")).equals("true"))
125 permissionString[4] = 'w';
126 if(values.elementAt(fields.indexOf("groupDelete")).equals("true"))
127 permissionString[5] = 'd';
128 if(values.elementAt(fields.indexOf("othersRead")).equals("true"))
129 permissionString[6] = 'r';
130 if(values.elementAt(fields.indexOf("othersWrite")).equals("true"))
131 permissionString[7] = 'w';
132 if(values.elementAt(fields.indexOf("othersDelete")).equals("true"))
133 permissionString[8] = 'd';
134 return this.getAllPermissions(new String(permissionString));
135 }
136
137 /**
138 * This method takes in a string and returns the int that would be stored in
139 * the database.
140 * @param perm String of the form "rwdrwdrwd"
141 * @return int representing the permissionID in the database
142 */
143 public int getAllPermissions(String perm) {
144 try {
145 return DBUtils.instance().getPermissions(perm);
146 }
147 catch (SQLException ex) {
148 log.warn("Unexpected SQLException caught in getAllPermissions(String) : " + ex.getMessage());
149 return -1;
150 }
151
152 }
153
154 /**
155 * This method takes in an int and returns the permissions string that the int
156 * will represent in the database.
157 * @param permissionID int between 0 and 511
158 * @return String of the form "rwdrwdrwd" with dashes in the place of perms that
159 * are not held
160 */
161 public String getAllPermissions(int permissionID) {
162 try {
163 return DBUtils.instance().getPermissions(permissionID);
164 }
165 catch (SQLException ex) {
166 log.warn("Unexpected SQLException caught in getAllPermissions(int) : " + ex.getMessage());
167 return null;
168 }
169
170 }
171
172
173 /**
174 * Method that gets a vector of strings listing all possible permissions in the db.
175 * @return Vector containing strings listing every possible permission in the
176 * database of the form CanActionEntity (ex. CanCreateAssets)
177 */
178 public Vector getPermissionsList() {
179 try {
180 return DBUtils.instance().getPermissionsList();
181 }
182 catch (SQLException ex) {
183 return null;
184 }
185 }
186
187 /**
188 * Determines whether the specified user belongs to the specified group.
189 * @param userName String referring to the user in question
190 * @param groupName String referring to the group in question
191 * @return true if user is in collection, false otherwise
192 * @throws SQLException
193 */
194 public boolean isUserInGroup(String userName, String groupName) {
195 try {
196 DBUtils dbUtils = DBUtils.instance();
197 return dbUtils.isUserInGroup(userName, groupName);
198 }
199 catch (SQLException ex) {
200 return false;
201 }
202 }
203
204 /**
205 * This method takes in the permission ID and returns a vector of strings
206 * for all the permissions that ID entails.
207 * @param permissionID String, a string containing an integer between 0 and 511
208 * representing the permission ID in the db.
209 * @return Vector, a vector of strings listing the parameter IDs
210 */
211 public Vector getPermissionNamesFromID(String permissionID) {
212 int permNum;
213 permNum = Integer.parseInt(permissionID);
214 Vector permissionsList = new Vector();
215 String binary = Integer.toBinaryString(permNum);
216 for (int i = 0 ; i < binary.length(); i++) {
217 if (binary.charAt(binary.length()-i-1) == '1' && i == 0)
218 permissionsList.addElement("ownerRead");
219 if (binary.charAt(binary.length()-i-1) == '1' && i == 1)
220 permissionsList.addElement("ownerWrite");
221 if (binary.charAt(binary.length()-i-1) == '1' && i == 2)
222 permissionsList.addElement("ownerDelete");
223 if (binary.charAt(binary.length()-i-1) == '1' && i == 3)
224 permissionsList.addElement("groupRead");
225 if (binary.charAt(binary.length()-i-1) == '1' && i == 4)
226 permissionsList.addElement("groupWrite");
227 if (binary.charAt(binary.length()-i-1) == '1' && i == 5)
228 permissionsList.addElement("groupDelete");
229 if (binary.charAt(binary.length()-i-1) == '1' && i == 6)
230 permissionsList.addElement("othersRead");
231 if (binary.charAt(binary.length()-i-1) == '1' && i == 7)
232 permissionsList.addElement("othersWrite");
233 if (binary.charAt(binary.length()-i-1) == '1' && i == 8)
234 permissionsList.addElement("othersDelete");
235 }
236 return permissionsList;
237 }
238
239
240
241 } // end of class