1
10package org.jstk.uam;
11
12import java.io.InputStreamReader;
13import java.io.BufferedReader;
14import java.util.Iterator;
15import java.security.Principal;
16import org.jstk.*;
17
18public class UAMShell extends JSTKAbstractTool {
19 private static UserAccountManager uam;
20
21 static class AddUserCommand extends JSTKCommandAdapter {
22 public Object execute(JSTKArgs args) throws JSTKException {
23 if (args.getNum() < 1)
24 return new JSTKResult(null, false, "No Login Name. AddUser failed.");
25 String loginName = args.get(0);
26 String userName = "";
27 String passWord = "default";
28 if (args.getNum() > 1)
29 userName = args.get(1);
30 if (args.getNum() > 2)
31 passWord = args.get(2);
32 uam.addUser(loginName, userName, passWord);
33 return new JSTKResult(null, true, "User Added: " + loginName);
34 }
35 }
36
37 static class RemUserCommand extends JSTKCommandAdapter {
38 public Object execute(JSTKArgs args) throws JSTKException {
39 if (args.getNum() < 1)
40 return new JSTKResult(null, false, "No Login Name. RemUser failed.");
41 String loginName = args.get(0);
42 if (uam.getUser(loginName) == null){
43 return new JSTKResult(null, true, "No such user: " + loginName);
44 } else {
45 uam.remUser(loginName);
46 return new JSTKResult(null, true, "User removed: " + loginName);
47 }
48 }
49 }
50
51 static class AddRoleCommand extends JSTKCommandAdapter {
52 public Object execute(JSTKArgs args) throws JSTKException {
53 if (args.getNum() < 1)
54 return new JSTKResult(null, false, "No Role Name. AddRole failed.");
55 String roleName = args.get(0);
56 String roleDesc = "";
57 if (args.getNum() > 1)
58 roleDesc = args.get(1);
59 uam.addRole(roleName, roleDesc);
60 return new JSTKResult(null, true, "Role Added: " + roleName);
61 }
62 }
63
64 static class RemRoleCommand extends JSTKCommandAdapter {
65 public Object execute(JSTKArgs args) throws JSTKException {
66 if (args.getNum() < 1)
67 return new JSTKResult(null, false, "No Login Name. RemRole failed.");
68 String roleName = args.get(0);
69 if (uam.getRole(roleName) == null){
70 return new JSTKResult(null, true, "No such role: " + roleName);
71 } else {
72 try {
73 uam.remRole(roleName);
74 } catch (UserAccountManager.RoleNotFreeException e){
75 return new JSTKResult(null, true, "Role has users: " + roleName);
76 }
77 return new JSTKResult(null, true, "Role removed: " + roleName);
78 }
79 }
80 }
81
82 static class AssignRoleCommand extends JSTKCommandAdapter {
83 public Object execute(JSTKArgs args) throws JSTKException {
84 if (args.getNum() < 1)
85 return new JSTKResult(null, false, "No Role Name. assignrole failed.");
86 if (args.getNum() < 2)
87 return new JSTKResult(null, false, "No User Name. assignrole failed.");
88 String roleName = args.get(0);
89 String loginName = args.get(1);
90
91 try {
92 uam.addRoleToUser(roleName, loginName);
93 } catch (UserAccountManager.NoSuchRoleException e){
94 return new JSTKResult(null, false, "Non-existent Role: " + roleName);
95 } catch (UserAccountManager.NoSuchUserException e){
96 return new JSTKResult(null, false, "Non-existent User: " + loginName);
97 }
98 return new JSTKResult(null, true, "Role " + roleName + " added to User " + loginName);
99 }
00 }
01
02 static class UnassignRoleCommand extends JSTKCommandAdapter {
03 public Object execute(JSTKArgs args) throws JSTKException {
04 if (args.getNum() < 1)
05 return new JSTKResult(null, false, "No Role Name. unassignrole failed.");
06 if (args.getNum() < 2)
07 return new JSTKResult(null, false, "No User Name. unassignrole failed.");
08 String roleName = args.get(0);
09 String loginName = args.get(1);
10
11 try {
12 uam.remRoleFromUser(roleName, loginName);
13 } catch (UserAccountManager.NoSuchRoleException e){
14 return new JSTKResult(null, false, "Non-existent Role: " + roleName);
15 } catch (UserAccountManager.NoSuchUserException e){
16 return new JSTKResult(null, false, "Non-existent User: " + loginName);
17 }
18 return new JSTKResult(null, true, "Role " + roleName + " removed from User " + loginName);
19 }
20 }
21
22 static class RolesCommand extends JSTKCommandAdapter {
23 public Object execute(JSTKArgs args) throws JSTKException {
24 try {
25 StringBuffer sb = new StringBuffer();
26 Iterator itr = uam.roles();
27 while (itr.hasNext()){
28 Principal roleP = (Principal)itr.next();
29 sb.append(roleP.getName() + ":");
30 Iterator itr1 = uam.roleUsers(roleP.getName());
31 while (itr1.hasNext()){
32 Principal userP = (Principal)itr1.next();
33 sb.append(" " + userP.getName());
34 }
35 sb.append("\n");
36 }
37 return new JSTKResult(null, true, "----- All Roles -----\n" + sb.toString());
38 } catch (UserAccountManager.NoSuchRoleException e){
39 return new JSTKResult(null, false, "internal inconsistency");
40 }
41 }
42 }
43
44 static class UsersCommand extends JSTKCommandAdapter {
45 public Object execute(JSTKArgs args) throws JSTKException {
46 try {
47 StringBuffer sb = new StringBuffer();
48 Iterator itr = uam.users();
49 while (itr.hasNext()){
50 Principal userP = (Principal)itr.next();
51 sb.append(userP.getName() + ":");
52 Iterator itr1 = uam.userRoles(userP.getName());
53 while (itr1.hasNext()){
54 Principal roleP = (Principal)itr1.next();
55 sb.append(" " + roleP.getName());
56 }
57 sb.append("\n");
58 }
59 return new JSTKResult(null, true, "----- All Users -----\n" + sb.toString());
60 } catch (UserAccountManager.NoSuchUserException e){
61 return new JSTKResult(null, false, "internal inconsistency");
62 }
63 }
64 }
65
66 static class UserRolesCommand extends JSTKCommandAdapter {
67 public Object execute(JSTKArgs args) throws JSTKException {
68 if (args.getNum() < 1)
69 return new JSTKResult(null, false, "No Login Name. userroles failed.");
70 String loginName = args.get(0);
71 StringBuffer sb = new StringBuffer();
72 Iterator itr = null;
73 try {
74 itr = uam.userRoles(loginName);
75 } catch (UserAccountManager.NoSuchUserException e){
76 return new JSTKResult(null, false, "No Such User: " + loginName);
77 }
78 while (itr.hasNext()){
79 Principal role = (Principal)itr.next();
80 sb.append(role.getName() + "\n");
81 }
82 return new JSTKResult(null, true, "All Roles:\n" + sb.toString());
83 }
84 }
85
86 static class ValidateCommand extends JSTKCommandAdapter {
87 public Object execute(JSTKArgs args) throws JSTKException {
88 if (args.getNum() < 1)
89 return new JSTKResult(null, false, "No Login Name. validate failed.");
90 String loginName = args.get(0);
91 if (args.getNum() < 2)
92 return new JSTKResult(null, false, "No Password. validate failed.");
93 String password = args.get(1);
94 try {
95 uam.validate(loginName, password);
96 } catch (UserAccountManager.NoSuchUserException e){
97 return new JSTKResult(null, false, "No Such User: " + loginName);
98 } catch (UserAccountManager.InvalidPasswordException e){
99 return new JSTKResult(null, false, "Invalid Password: " + password);
00 }
01 return new JSTKResult(null, true, "Validation SUCCESSFUL");
02 }
03 }
04
05 static class QuitCommand extends JSTKCommandAdapter {
06 public Object execute(JSTKArgs args) throws JSTKException {
07 System.exit(0);
08 return null;
09 }
10 }
11
12 static {
13 cmds.put("adduser", new AddUserCommand());
14 cmds.put("addrole", new AddRoleCommand());
15 cmds.put("remuser", new RemUserCommand());
16 cmds.put("remrole", new RemRoleCommand());
17 cmds.put("assignrole", new AssignRoleCommand());
18 cmds.put("unassignrole", new UnassignRoleCommand());
19 cmds.put("users", new UsersCommand());
20 cmds.put("roles", new RolesCommand());
21 cmds.put("userroles", new UserRolesCommand());
22 cmds.put("validate", new ValidateCommand());
23 cmds.put("quit", new QuitCommand());
24 cmds.put("exit", new QuitCommand());
25 }
26
27 public String progName(){
28 String progName = "java org.jstk.example.bank.BankClient";
29 return progName;
30 }
31 public String briefDescription(){
32 return "Client program for bank example";
33 }
34
35 public void init(UserAccountManager uam){
36 this.uam = uam;
37 }
38
39 public String execCommand(String[] args) throws Exception {
40 JSTKOptions opts = new JSTKOptions();
41 if (args.length < 1){ return usageString();
43 }
44 String cmdString = args[0];
45 if (cmdString.equals("-h") || cmdString.equals("help") || cmdString.equals("-?")){
46 return usageString();
47 }
48
49 JSTKCommand cmd = (JSTKCommand)cmds.get(cmdString);
50 if (cmd == null){ System.out.println("Unknown Command: " + cmdString);
52 return usageString();
53 }
54
55 if (args.length > 1 && (args[1].equals("-h") || args[1].equals("help") || args[1].equals("-?"))){
56 return cmdUsageString(cmd, cmdString);
57 }
58
59 opts.parse(args, 1);
60
61 JSTKResult result = (JSTKResult)cmd.execute(opts);
62 return result.getText();
63 }
64
65 public static void main(String[] args) throws Exception {
66 JSTKOptions opts = new JSTKOptions();
67 opts.parse(args, 0);
68 String uamfile = opts.get("uamfile");
69 if (uamfile == null)
70 uamfile = "config/uamdb.ser";
71 UAMShell shell = new UAMShell();
72 DefaultUAMPersistenceManager pm = new DefaultUAMPersistenceManager(uamfile);
73 shell.init(UserAccountManager.getInstance(pm));
74 while (true){
75 System.out.print("uam>");
76 System.out.flush();
77 String cmdline = new BufferedReader(new InputStreamReader(System.in)).readLine();
78 String[] cmdargs = cmdline.split("\\s");
79
80 String result = shell.execCommand(cmdargs);
81 System.out.println(result);
82 }
83 }
84}