ulrich
2016-12-27 b7910bcd54ccf93afd23f64b1dfa6df1565ba4f8
commit | author | age
6240cd 1 /*
U 2  Nutzerverwaltung - A Generic User Manager
3  Copyright (c) 2016  Ulrich Hilger
4
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Affero General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU Affero General Public License for more details.
14
15  You should have received a copy of the GNU Affero General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
c65695 18 package de.uhilger.um.api;
U 19
20 import de.uhilger.baselink.GenericRecord;
4f4b29 21 import de.uhilger.baselink.PersistenceManager;
c65695 22 import de.uhilger.baselink.Record;
1fc020 23 import de.uhilger.transit.web.WebKontext;
6240cd 24 import de.uhilger.um.Digester;
c65695 25 import de.uhilger.um.daten.User;
87e382 26 import de.uhilger.um.daten.UserRole;
4f4b29 27 import java.sql.Connection;
725d10 28 import java.util.List;
1fc020 29 import java.util.Properties;
U 30 import javax.servlet.ServletContext;
c65695 31
U 32 /**
1fc020 33  * <p>Die Methoden der Klasse UserMgr sind  
U 34  * die 'Fassade', die &uuml;ber Transit als Programmschnittstelle 
35  * (Application Programming Interface, API) 
36  * nach au&szlig;en bereitgestellt wird. UserMgr dient weitgehend 
37  * als 'Durchreiche' f&uuml;r die Funktionalit&auml;t anderer Klassen.</p>
59f8b3 38  * 
1fc020 39  * <p>
U 40  * Der UserMgr erh&auml;lt die ben&ouml;tigten Methoden aus dem ServletContext. 
41  * Auf diese Weise sind keine statischen Abh&auml;ngigkeiten in den Code 
42  * gewandert. Die folgenden Elemente werden vom UserMgr im ServletContext 
43  * erwartet:
44  * 
45  * [noch auspraegen]
46  * 
47  * PersistenceManager
48  * SQL-Properties
49  * Digester
50  * </p> 
627850 51  * 
6240cd 52  * @author Copyright (c) Ulrich Hilger, http://uhilger.de
U 53  * @author Published under the terms and conditions of the
54  * <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero
55  * General Public License</a>
56  *
57  * @version 2, December 27, 2016
c65695 58  */
1fc020 59 public class UserMgr implements WebKontext {
U 60   
61   private ServletContext ctx;
c65695 62   
U 63   private static final Record UserMapper = new GenericRecord(User.class);
87e382 64   private static final Record UserRoleMapper = new GenericRecord(UserRole.class);
1fc020 65   
U 66   /** Name, unter dem das Properties-Objekt mit den SQL-Befehlen im ServletContext hinterlegt ist */
67   public static final String UM_SQL_PROPERTIES = "umSqlProperties";
68
35c04d 69   public static final String P_DIGESTER = "digester";
U 70
1fc020 71   /** Name, unter dem das Zugriffsobjekt zur Datenbank im ServletContext hinterlegt ist */
U 72   public static final String UM_DB = "umDb";
c65695 73   
35c04d 74   /** Boolean-Konstante zur Kennzeichnung von Datenbankergebnissen ohne Blobs */
U 75   public static final boolean WITHOUT_BLOBS = false;
76
725d10 77   public static final String SQL_GET_USER_LIST = "getUserList";
U 78   public static final String SQL_GET_USER_NAME_LIST = "getUserNameList";
87e382 79   public static final String SQL_GET_ROLE_LIST = "getRoleList";
c79c12 80   public static final String SQL_GET_USER_ROLES = "getUserRoles";
72c5c3 81   public static final String SQL_DELETE_USER = "deleteUser";
4f4b29 82   public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles";
725d10 83   
be9fa2 84   /* ----------- Benutzer -------------- */
U 85   
35c04d 86   public User createUser(User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
c65695 87     String kw = user.getPw();
35c04d 88     String digesterClassName = ctx.getInitParameter(P_DIGESTER);
U 89     Digester digester = (Digester) Class.forName(digesterClassName).newInstance();
90     String digestedPw = digester.digest(kw, Digester.MD5, null);
c65695 91     user.setPw(digestedPw);
35c04d 92     getDb().insert(user, UserMapper);
c65695 93     return user;
U 94   }
95   
725d10 96   public List getUserNameList() {
35c04d 97     return getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS);
725d10 98   }
U 99   
72c5c3 100   public User deleteUser(User user) {
35c04d 101     PersistenceManager pm = getDb();
4f4b29 102     Connection c = pm.getConnection();
U 103     pm.startTransaction(c);
35c04d 104     pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId());
4f4b29 105     User deletedUser = (User) pm.delete(c, user, UserMapper);
U 106     pm.commit(c);
107     return deletedUser;
72c5c3 108   }
627850 109     
be9fa2 110   /* ------------ Rollen ------------------ */
U 111   
87e382 112   public UserRole grantRole(String userId, String roleName) {
U 113     UserRole ur = new UserRole();
114     ur.setRole(roleName);
115     ur.setUser(userId);
35c04d 116     getDb().insert(ur, UserRoleMapper);
87e382 117     return ur;
be9fa2 118   }
U 119   
87e382 120   public UserRole revokeRole(String userId, String roleName) {
U 121     UserRole ur = new UserRole();
122     ur.setRole(roleName);
123     ur.setUser(userId);
35c04d 124     getDb().delete(ur, UserRoleMapper);
87e382 125     return ur;
be9fa2 126   }
U 127   
128   public List getRoleNamesGranted() {
35c04d 129     return getDb().select(getSql(SQL_GET_ROLE_LIST), WITHOUT_BLOBS);
be9fa2 130   }
U 131   
c79c12 132   public List getUserRoleNames(String userId) {
35c04d 133     return getDb().select(getSql(SQL_GET_USER_ROLES), WITHOUT_BLOBS, userId);
f8b605 134   }
U 135   
1fc020 136   /* ----------- Helfer ---- */
U 137   
138   /**
139    * Ein benanntes SQL-Kommando ermitteln 
140    * @param id Name des gewuenschten SQL-Kommandos
141    * @return das SQL-Kommando mit der in id angegebenen Bezeichnung 
142    */
35c04d 143   private String getSql(String id) {
1fc020 144     Properties sql = (Properties) ctx.getAttribute(UserMgr.UM_SQL_PROPERTIES);
U 145     return sql.getProperty(id);
146   }
147   
35c04d 148   private PersistenceManager getDb() {
U 149     return (PersistenceManager) ctx.getAttribute(UM_DB);
150   }
151   
1fc020 152   /* ------------- Implementierung WebKontext ------------- */
U 153
154   @Override
155   public ServletContext getServletContext() {
156     return ctx;
157   }
158
159   @Override
160   public void setServletContext(ServletContext servletContext) {
161     this.ctx = servletContext;
162   }
163   
164   
c65695 165 }