Improvements in the user api

pull/30/head
Déborah Servili 2016-11-04 11:58:21 +01:00
parent dbba8ed0f9
commit a11e26f80b
5 changed files with 165 additions and 14 deletions

View File

@ -16,13 +16,12 @@ def init(url, key):
return PyMISP(url, key, True, 'json') return PyMISP(url, key, True, 'json')
if __name__ == '__main__': 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 = argparse.ArgumentParser(description='Add a new user by setting the mandory fields.')
parser.add_argument("-f", "--json_file", help="The name of the json file describing the user you want to create.") 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() args = parser.parse_args()
misp = init(misp_url, misp_key) misp = init(misp_url, misp_key)
if args.json_file is None: print (misp.add_user(args.email, args.org_id, args.role_id))
print (misp.get_add_user_fields_list())
else:
print(misp.add_user(args.json_file))

28
examples/add_user_json.py Executable file
View File

@ -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))

View File

@ -16,14 +16,11 @@ def init(url, key):
return PyMISP(url, key, True, 'json') return PyMISP(url, key, True, 'json')
if __name__ == '__main__': 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("-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() args = parser.parse_args()
misp = init(misp_url, misp_key) misp = init(misp_url, misp_key)
if args.json_file is None: print(misp.edit_user(args.user_id, email=args.email))
print (misp.get_edit_user_fields_list(args.user_id))
else:
print(misp.edit_user(args.json_file, args.user_id))

29
examples/edit_user_json.py Executable file
View File

@ -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))

View File

@ -1109,7 +1109,58 @@ class PyMISP(object):
response = session.get(url) response = session.get(url)
return self._check_response(response) 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() session = self.__prepare_session()
jdata = json.load(open(json_file)) jdata = json.load(open(json_file))
url = urljoin(self.root_url, 'admin/users/add/') url = urljoin(self.root_url, 'admin/users/add/')
@ -1122,7 +1173,54 @@ class PyMISP(object):
response = session.get(url) response = session.get(url)
return self._check_response(response) 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() session = self.__prepare_session()
jdata = json.load(open(json_file)) jdata = json.load(open(json_file))
url = urljoin(self.root_url, 'admin/users/edit/{}'.format(user_id)) url = urljoin(self.root_url, 'admin/users/edit/{}'.format(user_id))