2 * Copyright (c) 2016, 2017 Inocybe Technologies. and others. All rights reserved.
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
9 package org.opendaylight.aaa.cli.dmstore;
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;
22 * ChangeUserPassword change the user password.
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";
31 @Reference private IIDMStore identityStore;
33 @Option(name = "-user",
34 aliases = { "--userName" },
35 description = "The user name",
38 private String userName;
40 @Option(name = "-pass",
41 description = "User's Current Password",
45 private String currentPwd;
47 @Option(name = "-newPass",
48 description = "New Password",
52 private String newPwd;
54 @Reference private PasswordHashService passwordService;
57 public Object execute() throws Exception {
58 if (identityStore == null) {
59 return "Failed to access the users data store";
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";
70 return CHANGE_PASSWORD_FAIL;