adjust to use password-service
[aaa.git] / aaa-cli / src / main / java / org / opendaylight / aaa / cli / AaaCliAbstractCommand.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 package org.opendaylight.aaa.cli;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.Objects;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.aaa.api.IIDMStore;
14 import org.opendaylight.aaa.api.model.User;
15 import org.opendaylight.aaa.api.password.service.PasswordHashService;
16 import org.opendaylight.aaa.cli.utils.CliUtils;
17 import org.opendaylight.aaa.cli.utils.DataStoreUtils;
18
19 /**
20  * Base class for all CLI commands.
21  *
22  * @author mserngawy
23  *
24  */
25 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
26 public abstract class AaaCliAbstractCommand extends OsgiCommandSupport {
27
28     private static volatile String authUser = null;
29     protected IIDMStore identityStore;
30     private final PasswordHashService passwordService;
31
32     public AaaCliAbstractCommand(final PasswordHashService passwordService) {
33         Objects.requireNonNull(this.passwordService = passwordService);
34     }
35
36     public void setIdentityStore(IIDMStore identityStore) {
37         this.identityStore = identityStore;
38     }
39
40     @Override
41     protected Object doExecute() throws Exception {
42         final User currentUser = SessionsManager.getInstance().getCurrentUser(authUser);
43         if (currentUser == null) {
44             final String userName = CliUtils.readPassword(super.session, "Enter Username:");
45             final String passwd = CliUtils.readPassword(super.session, "Enter Password:");
46             final User usr = DataStoreUtils.isAdminUser(identityStore, passwordService, userName, passwd);
47             if (usr != null) {
48                 authUser = userName;
49                 SessionsManager.getInstance().addUserSession(userName, usr);
50             }
51             return usr;
52         }
53         return currentUser;
54     }
55 }