e1f15b8ee607bec9d63bf4fb6a83a4ce6aafee2e
[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 org.apache.karaf.shell.console.OsgiCommandSupport;
11 import org.opendaylight.aaa.api.IIDMStore;
12 import org.opendaylight.aaa.api.model.User;
13 import org.opendaylight.aaa.cli.utils.CliUtils;
14 import org.opendaylight.aaa.cli.utils.DataStoreUtils;
15
16 /**
17  * Base class for all CLI commands.
18  *
19  * @author mserngawy
20  *
21  */
22 public abstract class AaaCliAbstractCommand extends OsgiCommandSupport {
23
24     private static volatile String authUser = null;
25     protected final IIDMStore identityStore;
26
27     public AaaCliAbstractCommand(final IIDMStore identityStore) {
28         this.identityStore = identityStore;
29     }
30
31     @Override
32     protected Object doExecute() throws Exception {
33         final User currentUser = SessionsManager.getInstance().getCurrentUser(authUser);
34         if (currentUser == null) {
35             final String userName = CliUtils.readPassword(super.session, "Enter Username:");
36             final String passwd = CliUtils.readPassword(super.session, "Enter Password:");
37             final User usr = DataStoreUtils.isAdminUser(identityStore, userName, passwd);
38             if (usr != null) {
39                 authUser = userName;
40                 SessionsManager.getInstance().addUserSession(userName, usr);
41             }
42             return usr;
43         }
44         return currentUser;
45     }
46 }