Commit a11937f5 authored by Pim Schellart's avatar Pim Schellart
Browse files

Implement basic user management client.

parent 1b822bbe
Pipeline #802 skipped
# 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))
../vlbi.py
\ No newline at end of file
# 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 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("-i", "--info", action='store_true', help="Display user info")
parser.add_argument("-f", "--facility", nargs='*')
parser.add_argument("-p", "--password")
parser.add_argument("--name", help="Real name", default="")
parser.add_argument("--email", help="Email address", default="")
parser.add_argument("-U", "--admin-username", default="admin", help="Username of server admin")
parser.add_argument("-P", "--admin-password", default="admin", help="Password of server admin")
parser.add_argument("--server", help="Server address", default="http://localhost:8000/rpc")
args = parser.parse_args()
client = vlbi.Client(args.server, args.facility, args.admin_username, args.admin_password)
if args.add:
permissions = {"all": {"all": True}}
user = {"username": args.username,
"password": args.password,
"name": args.name,
"email": args.email,
"permissions": permissions}
print user
try:
client.addUser(user)
except vlbi.ServerError as e:
print e
if args.remove:
try:
client.removeUser(args.username)
except vlbi.ServerError as e:
print e
if args.info:
try:
print client.getUser(args.username)
except vlbi.ServerError as e:
print e
...@@ -48,6 +48,13 @@ class Client(object): ...@@ -48,6 +48,13 @@ class Client(object):
def auth(self): def auth(self):
return {"facility": self.facility, "username": self.username, "password": self.password} return {"facility": self.facility, "username": self.username, "password": self.password}
"""List all valid methods.
"""
def listMethods(self):
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "listMethods", "id": 0}
return self.post(request)
"""Send new data record to server. At the server this new data record is merged with current record and stored. """Send new data record to server. At the server this new data record is merged with current record and stored.
""" """
def updateRecord(self, record): def updateRecord(self, record):
...@@ -110,26 +117,43 @@ class Client(object): ...@@ -110,26 +117,43 @@ class Client(object):
return self.post(request) return self.post(request)
"""Add user """Change user password.
""" """
def addUser(self, user): def changePassword(self, username, password):
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "addUsers", "params": {"users": [user]}, "id": 0} user = {"username" : username, "password": password}
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "changePassword", "params": {"user": user}, "id": 0}
self.post(request) self.post(request)
"""Remove user """Add user.
Note that only admins can do this.
"""
def addUser(self, username, password, name=None, email=None, permissions=None):
user = {"username": username, "password": password,
"name": name, "email": email, "permissions": permissions}
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "addUser", "params": {"user": user}, "id": 0}
self.post(request)
"""Remove user.
Note that only admins can do this.
""" """
def removeUser(self, username): def removeUser(self, username):
user = {"username" : username} user = {"username" : username}
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "removeUsers", "params": {"users": [user]}, "id": 0} request = {"jsonrpc": "2.0", "auth": self.auth, "method": "removeUser", "params": {"user": user}, "id": 0}
self.post(request) self.post(request)
"""Get user info """Get user info.
Note that only admins can do this.
""" """
def getUser(self, username): def getUser(self, username):
user = {"username" : username} user = {"username" : username}
request = {"jsonrpc": "2.0", "auth": self.auth, "method": "getUsers", "params": {"users": [user]}, "id": 0} request = {"jsonrpc": "2.0", "auth": self.auth, "method": "getUser", "params": {"user": user}, "id": 0}
return self.post(request) return self.post(request)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment