Bump MRI upstreams
[aaa.git] / aaa-cli / src / main / java / org / opendaylight / aaa / cli / dmstore / ChangeUserPassword.java
1 /*
2  * Copyright (c) 2016, 2017 Inocybe Technologies. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.cli.dmstore;
10
11 import org.apache.karaf.shell.api.action.Action;
12 import org.apache.karaf.shell.api.action.Command;
13 import org.apache.karaf.shell.api.action.Option;
14 import org.apache.karaf.shell.api.action.lifecycle.Reference;
15 import org.apache.karaf.shell.api.action.lifecycle.Service;
16 import org.opendaylight.aaa.api.IIDMStore;
17 import org.opendaylight.aaa.api.model.User;
18 import org.opendaylight.aaa.api.model.Users;
19 import org.opendaylight.aaa.api.password.service.PasswordHashService;
20
21 /**
22  * ChangeUserPassword change the user password.
23  *
24  * @author mserngawy
25  */
26 @Service
27 @Command(name = "change-user-pwd", scope = "aaa", description = "Change the user password.")
28 public class ChangeUserPassword implements Action {
29     public static final String CHANGE_PASSWORD_FAIL = "Wrong username or current password";
30
31     @Reference private IIDMStore identityStore;
32
33     @Option(name = "-user",
34             aliases = { "--userName" },
35             description = "The user name",
36             required = true,
37             multiValued = false)
38     private String userName;
39
40     @Option(name = "-pass",
41             description = "User's Current Password",
42             required = true,
43             censor = true,
44             multiValued = false)
45     private String currentPwd;
46
47     @Option(name = "-newPass",
48             description = "New Password",
49             required = true,
50             censor = true,
51             multiValued = false)
52     private String newPwd;
53
54     @Reference private PasswordHashService passwordService;
55
56     @Override
57     public Object execute() throws Exception {
58         if (identityStore == null) {
59             return "Failed to access the users data store";
60         }
61         final Users users = identityStore.getUsers();
62         for (User usr : users.getUsers()) {
63             if (usr.getName().equals(userName)
64                     && passwordService.passwordsMatch(currentPwd, usr.getPassword(), usr.getSalt())) {
65                 usr.setPassword(newPwd);
66                 identityStore.updateUser(usr);
67                 return userName + "'s password has been changed";
68             }
69         }
70         return CHANGE_PASSWORD_FAIL;
71     }
72 }