ulrich
2017-04-29 8718969342fc65ab4c03374238d1199aa1f4470c
commit | author | age
48a649 1 /*
U 2  *  Nutzerverwaltung - User and role management in your browser
3  *  Copyright (C) 2011-2017 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/
17  */
18
19
20 package de.uhilger.um.api;
21
22 import de.uhilger.baselink.PersistenceManager;
23 import de.uhilger.baselink.Record;
24 import de.uhilger.um.Digester;
25 import static de.uhilger.um.api.UserMgr.MP_USER;
26 import static de.uhilger.um.api.UserMgr.P_DIGESTER;
27 import de.uhilger.um.daten.User;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 /**
33  * API-Methoden fuer die Aenderung der eigenen Nutzerdaten
34  */
35 public class Profil extends Api {
36   private static final Logger logger = Logger.getLogger(Profil.class.getName());
37   
38   public static final String SQL_GET_USER = "getUser";
39
40   public String setUserPw(String userId, String currentPw, String newPw) {
41     String result = "Kennwort nicht geƤndert";
42     try {
43     
44       String digesterClassName = getServletContext().getInitParameter(P_DIGESTER);
45       Digester digester = (Digester) Class.forName(digesterClassName).newInstance();
46       /*
47       MD5 geht nicht mehr,
48       vgl. http://stackoverflow.com/questions/39967289/how-to-use-digest-authentication-in-tomcat-8-5
49       */
50       String digestedCurrentPw = digester.digest(currentPw, Digester.SHA256, null);
51       PersistenceManager pm = getDb();
52       logger.fine(getSql(SQL_GET_USER));
53       List list = pm.select(getSql(SQL_GET_USER), getMapper(MP_USER), Record.WITHOUT_BLOBS, userId);
54       if(list != null && list.size() > 0) {
55         Object o = list.get(0);
56         if(o instanceof User) {
57           User u = (User) o;
58           if(u.getPw().equals(digestedCurrentPw)) {
59             String digestedNewPw = digester.digest(newPw, Digester.SHA256, null);
60             u.setPw(digestedNewPw);
61             pm.update(u, getMapper(MP_USER));
62             result = "Kennwort geaendert";
63           } else {
64             result = "Das Kennwort ist falsch";
65           }
66         }
67       } else {
68         result = "Benutzer " + userId + " nicht gefunden";
69       }
70     } catch (ClassNotFoundException|InstantiationException|IllegalAccessException ex) {
71       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
72     }
73     return result;
74   }
75 }