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