adduser.py 2.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# An example client for the Radboud Radio Lab VLBI Monitor
# 
# Copyright (c) 2016 Pim Schellart
# 
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.

import vlbi
import getpass

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("username")
parser.add_argument("-a", "--add", action='store_true')
parser.add_argument("-r", "--remove", action='store_true')
parser.add_argument("-g", "--get_info", action='store_true')
parser.add_argument("-c", "--change-password", action='store_true')
parser.add_argument("-f", "--facility", nargs="+", help="One or more facilities")
parser.add_argument("--name", help="Real name", default="")
parser.add_argument("--email", help="Email address", default="")
parser.add_argument("-U", "--server-admin", default="admin", help="Username of server admin")
parser.add_argument("-S", "--server", default="http://localhost:8000/rpc", help="Server address")

args = parser.parse_args()

if args.add:
    server_password = getpass.getpass("Password for {0}:".format(args.server_admin))
    
    client = vlbi.Client(args.server, None, args.server_admin, server_password, backup=False)

    permissions = {}
    for method in client.listMethods():
        permissions[method] = {}
        for facility in args.facility:
            permissions[method][facility] = True

    permissions["changePassword"] = {"*": True}

    password = getpass.getpass("New user password:")
    try:
        client.addUser(args.username, password, name=args.name, email=args.email, permissions=permissions)
    except vlbi.ServerError as e:
        print "Error: {0}".format(str(e))

if args.remove:
    server_password = getpass.getpass("Password for {0}:".format(args.server_admin))
    
    client = vlbi.Client(args.server, None, args.server_admin, server_password, backup=False)

    try:
        client.removeUser(args.username)
    except vlbi.ServerError as e:
        print e

if args.get_info:
    server_password = getpass.getpass("Password for {0}:".format(args.server_admin))
    
    client = vlbi.Client(args.server, None, args.server_admin, server_password, backup=False)

    try:
        info = client.getUser(args.username)

        print "Username:", info["username"]
        print "Permissions:", info["permissions"]
        print "Is admin:", info["isAdmin"]

    except vlbi.ServerError as e:
        print e

if args.change_password:
    old_password = getpass.getpass("Password for {0}:".format(args.username))
    new_password = getpass.getpass("New password:")
    
    client = vlbi.Client(args.server, None, args.username, old_password, backup=False)

    try:
        client.changePassword(args.username, new_password)
    except vlbi.ServerError as e:
        print "Error: {0}".format(str(e))