diff --git a/modules/eeuser/src/main/java/org/jpos/ee/RoleManager.java b/modules/eeuser/src/main/java/org/jpos/ee/RoleManager.java new file mode 100644 index 0000000000..20c4b8c7dc --- /dev/null +++ b/modules/eeuser/src/main/java/org/jpos/ee/RoleManager.java @@ -0,0 +1,31 @@ +/* + * jPOS Project [http://jpos.org] + * Copyright (C) 2000-2016 Alejandro P. Revilla + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.jpos.ee; + +public class RoleManager { + private DB db; + public RoleManager (DB db) { + this.db = db; + } + + public Role getRoleByName (String name) { + return (Role) db.session().createQuery("from Role c WHERE c.name=:name"). + setParameter("name", name).uniqueResult(); + } +} diff --git a/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDROLE.java b/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDROLE.java new file mode 100644 index 0000000000..c1b9420e8d --- /dev/null +++ b/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDROLE.java @@ -0,0 +1,41 @@ +package org.jpos.q2.cli; + +import org.jpos.ee.DB; +import org.jpos.ee.Permission; +import org.jpos.ee.Role; +import org.jpos.ee.RoleManager; +import org.jpos.q2.CLICommand; +import org.jpos.q2.CLIContext; + +import java.util.Set; + +@SuppressWarnings("unused") +public class ADDROLE implements CLICommand { + + @Override + public void exec(CLIContext cli, String[] args) throws Exception { + if (args.length < 2) { + cli.println("Usage: addrole ... "); + return; + } + try (DB db = new DB()) { + db.open(); + db.beginTransaction(); + RoleManager rm = new RoleManager(db); + Role role = null; + if (rm.getRoleByName(args[1]) == null) { + + + role = new Role(args[1]); + Set perms = role.getPermissions(); + for (int i = 2; i < args.length; i++) + perms.add(Permission.valueOf(args[i])); + db.save(role); + } + db.commit(); + cli.println (role != null ? "Role created " + role.getName() : " Role already exists"); + } catch (Exception e) { + cli.println (e.getMessage()); + } + } +} diff --git a/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDUSER.java b/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDUSER.java new file mode 100644 index 0000000000..05bd2560a8 --- /dev/null +++ b/modules/eeuser/src/main/java/org/jpos/q2/cli/ADDUSER.java @@ -0,0 +1,90 @@ +package org.jpos.q2.cli; + +import org.apache.commons.cli.*; +import org.jpos.ee.*; +import org.jpos.q2.CLICommand; +import org.jpos.q2.CLIContext; + +import java.io.PrintWriter; +import java.util.Collections; + +@SuppressWarnings("unused") +public class ADDUSER implements CLICommand { + @Override + public void exec(CLIContext cli, String[] args) throws Exception { + CommandLineParser parser = new DefaultParser(); + Options options = new Options(); + options.addOption ("n", "name", true, "User's full name"); + options.addOption ("p", "password", true, "The password"); + options.addOption ("r", "role", true, "User role"); + options.addOption ("h", "help", false, "This help"); + CommandLine line = parser.parse(options, shl(shl(args))); + + if (args.length < 2 || line.hasOption('h')) { + showHelp (cli, args, options); + return; + } + if (!line.hasOption('n')) { + cli.println ("--nick parameter is required. See --help for help"); + return; + } + try (DB db = new DB()) { + db.open(); + db.beginTransaction(); + UserManager mgr = new UserManager(db); + User u = mgr.getUserByNick(args[1], true); + if (u != null) { + cli.println ("User already exists" + (u.isDeleted() ? " (soft-deleted)" : "")); + return; + } + RoleManager rm = new RoleManager(db); + Role[] rr = null; + if (line.hasOption('r')) { + String[] roles = line.getOptionValues('r'); + rr = new Role[roles.length]; + int i=0; + for (String s : roles) { + Role r = rm.getRoleByName(s); + if (r == null) { + cli.println ("Role '" + s + "' does not exist - aborting"); + return; + } + rr[i++] = r; + } + } + User user = new User(); + user.setNick (args[1]); + user.setName (line.getOptionValue('N')); + db.session().save(user); + if (line.hasOption('p')) { + mgr.setPassword(user, line.getOptionValue('p')); + } + if (rr != null) + Collections.addAll(user.getRoles(), rr); + user.setActive(true); + db.commit(); + cli.println ("User '" + user.getNickAndId() + "' created"); + } catch (Exception e) { + cli.println (e.getMessage()); + } + } + + private String[] shl (String[] args) { + switch (args.length) { + case 0: + return args; + case 1: + return new String[0]; + } + String[] shl = new String[args.length-1]; + System.arraycopy(args,1, shl, 0, args.length-1); + return shl; + } + + private void showHelp (CLIContext cli, String args[], Options options) { + PrintWriter pw = new PrintWriter (cli.getOutputStream()); + HelpFormatter helpFormatter = new HelpFormatter (); + helpFormatter.printHelp (args[0] + "", options); + helpFormatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, args[0], null, options, 0, HelpFormatter.DEFAULT_DESC_PAD, null,false); + } +} diff --git a/modules/eeuser/src/main/java/org/jpos/q2/cli/RMROLE.java b/modules/eeuser/src/main/java/org/jpos/q2/cli/RMROLE.java new file mode 100644 index 0000000000..8b239c9169 --- /dev/null +++ b/modules/eeuser/src/main/java/org/jpos/q2/cli/RMROLE.java @@ -0,0 +1,30 @@ +package org.jpos.q2.cli; + +import org.jpos.ee.DB; +import org.jpos.ee.Role; +import org.jpos.ee.RoleManager; +import org.jpos.q2.CLICommand; +import org.jpos.q2.CLIContext; + +@SuppressWarnings("unused") +public class RMROLE implements CLICommand { + @Override + public void exec(CLIContext cli, String[] args) throws Exception { + if (args.length != 2) { + cli.println("Usage: rmrole "); + return; + } + try (DB db = new DB()) { + db.open(); + db.beginTransaction(); + RoleManager rm = new RoleManager(db); + Role r = rm.getRoleByName(args[1]); + if (r != null) + db.session().delete(r); + db.commit(); + cli.println (r != null ? "Role " + r + " has been deleted" : "Role not found"); + } catch (Exception e) { + cli.println (e.getMessage()); + } + } +} diff --git a/modules/eeuser/src/main/java/org/jpos/q2/cli/RMUSER.java b/modules/eeuser/src/main/java/org/jpos/q2/cli/RMUSER.java new file mode 100644 index 0000000000..81aaa3ba99 --- /dev/null +++ b/modules/eeuser/src/main/java/org/jpos/q2/cli/RMUSER.java @@ -0,0 +1,29 @@ +package org.jpos.q2.cli; + +import org.jpos.ee.*; +import org.jpos.q2.CLICommand; +import org.jpos.q2.CLIContext; + +@SuppressWarnings("unused") +public class RMUSER implements CLICommand { + @Override + public void exec(CLIContext cli, String[] args) throws Exception { + if (args.length != 2) { + cli.println("Usage: rmuser "); + return; + } + try (DB db = new DB()) { + db.open(); + db.beginTransaction(); + UserManager mgr = new UserManager(db); + User u = mgr.getUserByNick(args[1]); + if (u != null) { + u.setDeleted(true); + } + db.commit(); + cli.println(u != null ? "User " + u.getNickAndId() + " has been deleted" : "User does not exist"); + } catch (Exception e) { + cli.println(e.getMessage()); + } + } +} diff --git a/modules/eeuser/src/main/resources/org/jpos/q2/cli/addrole.man b/modules/eeuser/src/main/resources/org/jpos/q2/cli/addrole.man new file mode 100644 index 0000000000..abe16fbfa4 --- /dev/null +++ b/modules/eeuser/src/main/resources/org/jpos/q2/cli/addrole.man @@ -0,0 +1 @@ +Usage: addrole ... diff --git a/modules/eeuser/src/main/resources/org/jpos/q2/cli/adduser.man b/modules/eeuser/src/main/resources/org/jpos/q2/cli/adduser.man new file mode 100644 index 0000000000..6c6f89cf73 --- /dev/null +++ b/modules/eeuser/src/main/resources/org/jpos/q2/cli/adduser.man @@ -0,0 +1,7 @@ +Usage: rmrole +q2> adduser +usage: adduser + -h,--help This help + -n,--name User's full name + -p,--password The password + -r,--role User role diff --git a/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmrole.man b/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmrole.man new file mode 100644 index 0000000000..e507f3b172 --- /dev/null +++ b/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmrole.man @@ -0,0 +1 @@ +Usage: rmrole diff --git a/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmuser.man b/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmuser.man new file mode 100644 index 0000000000..bbe6c73980 --- /dev/null +++ b/modules/eeuser/src/main/resources/org/jpos/q2/cli/rmuser.man @@ -0,0 +1 @@ +Usage: rmuser diff --git a/modules/sysconfig/src/main/java/org/jpos/ee/SysConfigManager.java b/modules/sysconfig/src/main/java/org/jpos/ee/SysConfigManager.java index e5c1ef8346..e1bc32e3b2 100644 --- a/modules/sysconfig/src/main/java/org/jpos/ee/SysConfigManager.java +++ b/modules/sysconfig/src/main/java/org/jpos/ee/SysConfigManager.java @@ -73,6 +73,22 @@ public String get (String name, String defaultValue) { } return defaultValue; } + + public boolean delete (String name) { + try { + if (prefix != null) + name = prefix + name; + SysConfig cfg = db.session().get (SysConfig.class, name); + if (cfg != null) { + db.session().delete(cfg); + return true; + } + } catch (HibernateException e) { + db.getLog().warn (e); + } + return false; + } + public SysConfig[] getAll (String queryString) { SysConfig[] values; try {