From a11e26f80bc937800e1cb057e5b996dde1db2494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A9borah=20Servili?= Date: Fri, 4 Nov 2016 11:58:21 +0100 Subject: [PATCH] Improvements in the user api --- examples/add_user.py | 11 ++-- examples/add_user_json.py | 28 ++++++++++ examples/edit_user.py | 9 ++-- examples/edit_user_json.py | 29 +++++++++++ pymisp/api.py | 102 ++++++++++++++++++++++++++++++++++++- 5 files changed, 165 insertions(+), 14 deletions(-) create mode 100755 examples/add_user_json.py create mode 100755 examples/edit_user_json.py diff --git a/examples/add_user.py b/examples/add_user.py index 6236f09..fbec04e 100755 --- a/examples/add_user.py +++ b/examples/add_user.py @@ -16,13 +16,12 @@ def init(url, key): return PyMISP(url, key, True, 'json') if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Add the user described in the given json. If no file is provided, returns a json listing all the fields used to describe a user.') - parser.add_argument("-f", "--json_file", help="The name of the json file describing the user you want to create.") + parser = argparse.ArgumentParser(description='Add a new user by setting the mandory fields.') + parser.add_argument("-e", "--email", required=True, help="Email linked to the account.") + parser.add_argument("-o", "--org_id", required=True, help="Organisation linked to the user.") + parser.add_argument("-r", "--role_id", required=True, help="Role linked to the user.") args = parser.parse_args() misp = init(misp_url, misp_key) - if args.json_file is None: - print (misp.get_add_user_fields_list()) - else: - print(misp.add_user(args.json_file)) + print (misp.add_user(args.email, args.org_id, args.role_id)) diff --git a/examples/add_user_json.py b/examples/add_user_json.py new file mode 100755 index 0000000..6f79cc1 --- /dev/null +++ b/examples/add_user_json.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from pymisp import PyMISP +from keys import misp_url, misp_key +import argparse + +# For python2 & 3 compat, a bit dirty, but it seems to be the least bad one +try: + input = raw_input +except NameError: + pass + + +def init(url, key): + return PyMISP(url, key, True, 'json') + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Add the user described in the given json. If no file is provided, returns a json listing all the fields used to describe a user.') + parser.add_argument("-f", "--json_file", help="The name of the json file describing the user you want to create.") + args = parser.parse_args() + + misp = init(misp_url, misp_key) + + if args.json_file is None: + print (misp.get_add_user_fields_list()) + else: + print(misp.add_user_json(args.json_file)) diff --git a/examples/edit_user.py b/examples/edit_user.py index f7b3d98..6d16ea9 100755 --- a/examples/edit_user.py +++ b/examples/edit_user.py @@ -16,14 +16,11 @@ def init(url, key): return PyMISP(url, key, True, 'json') if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Edit the user designed by the user_id. If no file is provided, returns a json listing all the fields used to describe a user.') + parser = argparse.ArgumentParser(description='Edit the email of the user designed by the user_id.') parser.add_argument("-i", "--user_id", required=True, help="The name of the json file describing the user you want to modify.") - parser.add_argument("-f", "--json_file", help="The name of the json file describing your modifications.") + parser.add_argument("-e", "--email", help="Email linked to the account.") args = parser.parse_args() misp = init(misp_url, misp_key) - if args.json_file is None: - print (misp.get_edit_user_fields_list(args.user_id)) - else: - print(misp.edit_user(args.json_file, args.user_id)) + print(misp.edit_user(args.user_id, email=args.email)) diff --git a/examples/edit_user_json.py b/examples/edit_user_json.py new file mode 100755 index 0000000..7c5deb8 --- /dev/null +++ b/examples/edit_user_json.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from pymisp import PyMISP +from keys import misp_url, misp_key +import argparse + +# For python2 & 3 compat, a bit dirty, but it seems to be the least bad one +try: + input = raw_input +except NameError: + pass + + +def init(url, key): + return PyMISP(url, key, True, 'json') + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Edit the user designed by the user_id. If no file is provided, returns a json listing all the fields used to describe a user.') + parser.add_argument("-i", "--user_id", required=True, help="The name of the json file describing the user you want to modify.") + parser.add_argument("-f", "--json_file", help="The name of the json file describing your modifications.") + args = parser.parse_args() + + misp = init(misp_url, misp_key) + + if args.json_file is None: + print (misp.get_edit_user_fields_list(args.user_id)) + else: + print(misp.edit_user_json(args.json_file, args.user_id)) diff --git a/pymisp/api.py b/pymisp/api.py index 701f19b..3b7eb04 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -1109,7 +1109,58 @@ class PyMISP(object): response = session.get(url) return self._check_response(response) - def add_user(self, json_file): + def add_user_json(self, json_file): + session = self.__prepare_session() + jdata = json.load(open(json_file)) + url = urljoin(self.root_url, 'admin/users/add/') + response = session.post(url, data=json.dumps(jdata)) + return self._check_response(response) + + def add_user(self, email, org_id, role_id, password=None, + external_auth_required=None, external_auth_key=None, + enable_password=None, nids_sid=None, server_id=None, + gpgkey=None, certif_public=None, autoalert=None, + contactalert=None, disabled=None, change_pw=None, + termsaccepted=None, newsread=None): + new_user = {} + new_user['email'] = email + new_user['org_id'] = org_id + new_user['role_id'] = role_id + if password is not None: + new_user['password'] = password + if external_auth_required is not None: + new_user['external_auth_required'] = external_auth_required + if external_auth_key is not None: + new_user['external_auth_key'] = external_auth_key + if enable_password is not None: + new_user['enable_password'] = enable_password + if nids_sid is not None: + new_user['nids_sid'] = nids_sid + if server_id is not None: + new_user['server_id'] = server_id + if gpgkey is not None: + new_user['gpgkey'] = gpgkey + if certif_public is not None: + new_user['certif_public'] = certif_public + if autoalert is not None: + new_user['autoalert'] = autoalert + if contactalert is not None: + new_user['contactalert'] = contactalert + if disabled is not None: + new_user['disabled'] = disabled + if change_pw is not None: + new_user['change_pw'] = change_pw + if termsaccepted is not None: + new_user['termsaccepted'] = termsaccepted + if newsread is not None: + new_user['newsread'] = newsread + + session = self.__prepare_session() + url = urljoin(self.root_url, 'admin/users/add/') + response = session.post(url, data=json.dumps(new_user)) + return self._check_response(response) + + def add_user_json(self, json_file): session = self.__prepare_session() jdata = json.load(open(json_file)) url = urljoin(self.root_url, 'admin/users/add/') @@ -1122,7 +1173,54 @@ class PyMISP(object): response = session.get(url) return self._check_response(response) - def edit_user(self, json_file, user_id): + def edit_user(self, user_id, email=None, org_id=None, role_id=None, + password=None, external_auth_required=None, + external_auth_key=None, enable_password=None, nids_sid=None, + server_id=None, gpgkey=None, certif_public=None, + autoalert=None, contactalert=None, disabled=None, + change_pw=None, termsaccepted=None, newsread=None): + edit_user = {} + if email is not None: + edit_user['email'] = email + if org_id is not None: + edit_user['org_id'] = org_id + if role_id is not None: + edit_user['role_id'] = role_id + if password is not None: + edit_user['password'] = password + if external_auth_required is not None: + edit_user['external_auth_required'] = external_auth_required + if external_auth_key is not None: + edit_user['external_auth_key'] = external_auth_key + if enable_password is not None: + edit_user['enable_password'] = enable_password + if nids_sid is not None: + edit_user['nids_sid'] = nids_sid + if server_id is not None: + edit_user['server_id'] = server_id + if gpgkey is not None: + edit_user['gpgkey'] = gpgkey + if certif_public is not None: + edit_user['certif_public'] = certif_public + if autoalert is not None: + edit_user['autoalert'] = autoalert + if contactalert is not None: + edit_user['contactalert'] = contactalert + if disabled is not None: + edit_user['disabled'] = disabled + if change_pw is not None: + edit_user['change_pw'] = change_pw + if termsaccepted is not None: + edit_user['termsaccepted'] = termsaccepted + if newsread is not None: + edit_user['newsread'] = newsread + + session = self.__prepare_session() + url = urljoin(self.root_url, 'admin/users/edit/{}'.format(user_id)) + response = session.post(url, data=json.dumps(edit_user)) + return self._check_response(response) + + def edit_user_json(self, json_file, user_id): session = self.__prepare_session() jdata = json.load(open(json_file)) url = urljoin(self.root_url, 'admin/users/edit/{}'.format(user_id))