summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2015-03-01 20:49:36 +0200
committerAlon Levy <alon@pobox.com>2015-03-01 20:51:16 +0200
commita186d588f166f1f45b3d3a2a9e875c86410dc581 (patch)
tree1396321a89f83d8c625b58b3286a825a662c0803
parent0ecd1c985bf758eebdce1ad2a83992b06b0d5c22 (diff)
rz_cli_tool: add role add and remove functionality
Fixes #148 Usage: ./src/local/rz_cli_tool.py --config-dir res/etc --user-db-path res/user_db.db --email alon@rhizi.local --role-add admin ./src/local/rz_cli_tool.py --config-dir res/etc --user-db-path res/user_db.db --email alon@rhizi.local --role-rm admin
-rw-r--r--README.md7
-rwxr-xr-xsrc/local/rz_cli_tool.py89
2 files changed, 68 insertions, 28 deletions
diff --git a/README.md b/README.md
index 9d31a815..17511ff5 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,13 @@ Command line use documentation can be view with:
## Configuration
Rhizi configuration is currently documented in code, see <code>src/server/rz_server.py#Config</code>
+## User addition
+Through the sign up page you can add users.
+
+## Changing users roles
+This is possible through the src/local/rz_cli_tool as follows:
+./src/local/rz_cli_tool.py --config-dir res/etc --user-db-path res/user_db.db --email alon@rhizi.local --role-add admin
+
# Development
Build currently uses Apache Ant(http://ant.apache.org/) and [make](https://www.gnu.org/software/make/)
diff --git a/src/local/rz_cli_tool.py b/src/local/rz_cli_tool.py
index 6f5ab933..3c98d45a 100755
--- a/src/local/rz_cli_tool.py
+++ b/src/local/rz_cli_tool.py
@@ -12,29 +12,28 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'server'))
from crypt_util import hash_pw
from rz_server import init_config
+from rz_user_db import User_Account, User_DB
+def init_pw_db(cfg, user_pw_list_file, user_db_path):
-def init_pw_db(cfg, user_pw_list_file):
+ def add_user_login(first_name, last_name,
+ rz_username, email_address, pw_plaintext):
+ pw_hash = hash_pw(str(pw_plaintext), salt)
+ u_account = User_Account(first_name=first_name,
+ last_name=last_name,
+ rz_username=rz_username,
+ email_address=email_address,
+ pw_hash=pw_hash,
+ role_set=['user'])
- def add_user_login(htpasswd_path, salt, u, p):
- with open(htpasswd_path, 'rb') as f:
- data = f.read()
- pw_db = pickle.loads(data)
+ print('user_db: added entry: user: %s, pw: %s...' % (u, pw_db[u][:5]))
- with open(htpasswd_path, 'wb') as f:
- pw_db[u] = hash_pw(str(p), salt)
- pickle.dump(pw_db, f)
-
- print('htpasswd db: added entry: user: %s, pw: %s...' % (u, pw_db[u][:5]))
-
- htpasswd_path = cfg.htpasswd_path
-
- if os.path.exists(htpasswd_path):
- print('htpasswd_path already exists, aborting: ' + htpasswd_path)
+ if os.path.exists(user_db_path):
+ print('user_db_path already exists, aborting: ' + user_db_path)
return
- with open(htpasswd_path, 'wb') as f: # init file
- pickle.dump({}, f)
+ user_db = User_DB(db_path=db_path)
+ user_db.init(mode='c')
salt = cfg.secret_key
assert len(salt) > 8, 'server-key not found or too short'
@@ -47,27 +46,61 @@ def init_pw_db(cfg, user_pw_list_file):
continue
kv_arr = line.split(',')
- if 2 != len(kv_arr):
- raise Exception('failed to parse user,pw line: ' + line)
+ if 5 != len(kv_arr):
+ raise Exception('failed to parse first-name,last-name,email,user,pw line: ' + line)
- user, pw = map(str.strip, kv_arr)
- add_user_login(htpasswd_path, salt, user, pw)
- print('added user: u: ' + user)
+ first_name, last_name, rz_username, email_address, pw_plaintext = map(
+ str.strip, kv_arr)
+ add_user_login(first_name=first_name,
+ last_name=last_name,
+ rz_username=rz_username,
+ email_address=email_address,
+ pw_plaintext=pw_plaintext)
+ print('added user: u: ' + rz_username)
u_count = u_count + 1
- print('htpasswd generated: path: %s, user-count: %d' % (htpasswd_path, u_count))
+ print('user_db generated: path: %s, user-count: %d' % (user_db_path, u_count))
+
+def open_existing_user_db(user_db_path):
+ user_db = User_DB(db_path=user_db_path)
+ user_db.init(mode='w')
+ return user_db
+
+def role_add(user_db_path, user_email, role):
+ user_db = open_existing_user_db(user_db_path)
+ uid, u = user_db.lookup_user__by_email_address(user_email)
+ user_db.user_add_role(uid, role)
+
+def role_rm(user_db_path, user_email, role):
+ user_db = open_existing_user_db(user_db_path)
+ uid, u = user_db.lookup_user__by_email_address(user_email)
+ user_db.user_rm_role(uid, role)
def main():
p = argparse.ArgumentParser(description='rz-cli tool')
p.add_argument('--config-dir', help='path to Rhizi config dir', default='res/etc')
- p.add_argument('--init-htpasswd-db', help='init login htpasswd db', action='store_const', const=True)
- p.add_argument('--htpasswd-init-file', help='htpasswd db initialization file in \'user,pw\' format')
+ p.add_argument('--user-db-path', help='path to user_db (ignore config)')
+ p.add_argument('--init-user-db', help='init user db', action='store_const', const=True)
+ p.add_argument('--user-db-init-file', help='user_db db initialization file in \'user,pw\' format')
+ p.add_argument('--email', help='email of user to operate on')
+ p.add_argument('--role-add', help='role to add to user, i.e. admin or user')
+ p.add_argument('--role-rm', help='role to remove from user')
args = p.parse_args()
- cfg = init_config(args.config_dir)
+ if None == args.user_db_path:
+ cfg = init_config(args.config_dir)
+ user_db_path = cfg.user_db_path
+ else:
+ user_db_path = args.user_db_path
- if args.init_htpasswd_db:
- init_pw_db(cfg, args.htpasswd_init_file)
+ if args.init_user_db:
+ init_pw_db(cfg, args.user_db_init_file, user_db_path)
exit(0)
+ if args.email:
+ if args.role_add:
+ role_add(user_db_path, args.email, args.role_add)
+ if args.role_rm:
+ role_rm(user_db_path, args.email, args.role_rm)
+
if __name__ == '__main__':
main()