ulrich@undisclosed
2020-05-17 f7254551bdefb51624b4a26940543235e07db48f
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       PersistenceManager pm = getDb();
47       logger.fine(getSql(SQL_GET_USER));
48       List list = pm.select(getSql(SQL_GET_USER), getMapper(MP_USER), Record.WITHOUT_BLOBS, userId);
49       if(list != null && list.size() > 0) {
50         Object o = list.get(0);
51         if(o instanceof User) {
52           User u = (User) o;
3ae5ee 53           if(digester.matches(currentPw, u.getPw(), Digester.SHA256, null)) {
48a649 54             String digestedNewPw = digester.digest(newPw, Digester.SHA256, null);
U 55             u.setPw(digestedNewPw);
56             pm.update(u, getMapper(MP_USER));
57             result = "Kennwort geaendert";
58           } else {
59             result = "Das Kennwort ist falsch";
60           }
61         }
62       } else {
63         result = "Benutzer " + userId + " nicht gefunden";
64       }
65     } catch (ClassNotFoundException|InstantiationException|IllegalAccessException ex) {
66       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
67     }
68     return result;
69   }
70 }