def edit_user_properties(username, **kwargs): """ Edit the properties of a customer. Args: username (str): Name of the user you are editing. Kwargs: full_name (str): The full name of the user email (str): The email address of the user user_name (str): The name of the user who called this function. current_customer (str): The name of the customer you want to manage. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Return: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'None - edit_user_properties - admin was updated', 'data': { 'full_name': 'vFense Admin' } } """ if not kwargs.get(ApiResultKeys.USERNAME): user_name = None else: user_name = kwargs.pop(ApiResultKeys.USERNAME) if not kwargs.get(ApiResultKeys.URI): uri = None else: uri = kwargs.pop(ApiResultKeys.URI) if not kwargs.get(ApiResultKeys.HTTP_METHOD): method = None else: method = kwargs.pop(ApiResultKeys.HTTP_METHOD) if kwargs.get(UserKeys.Password): kwargs.pop(UserKeys.Password) user_exist = get_user(username, without_fields=None) status = edit_user_properties.func_name + ' - ' generic_status_code = 0 vfense_status_code = 0 try: if user_exist: data_validated = _validate_user_data(username, **kwargs) if data_validated: object_status, _, _, _ = ( update_user(username, kwargs) ) if object_status == DbCodes.Replaced: msg = 'User %s was updated - ' % (username) generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = UserCodes.UserUpdated elif object_status == DbCodes.Unchanged: msg = 'User %s was not updated - ' % (username) generic_status_code = GenericCodes.ObjectUnchanged vfense_status_code = UserCodes.UserUnchanged else: object_status = DbCodes.Skipped generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.FailedToUpdateUser msg = 'User %s properties were invalid - ' % (username) else: object_status = DbCodes.Skipped generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.UserNameDoesNotExist msg = 'User %s does not exist - ' % (username) results = { ApiResultKeys.DB_STATUS_CODE: object_status, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.UPDATED_IDS: username, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [kwargs], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } except Exception as e: logger.exception(e) generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = UserFailureCodes.FailedToUpdateUser msg = 'Failed to update properties for user %s: %s' % (username, str(e)) results = { ApiResultKeys.DB_STATUS_CODE: DbCodes.Errors, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [kwargs], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return(results)
def change_password( username, password, new_password, user_name=None, uri=None, method=None ): """Change password for a user. Args: username (str): The name of the user you are deleteing. password (str): Original password. new_password (str): New password. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Return: Dictionary of the status of the operation. { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'None - change_password - Password changed for user admin - admin was updated', 'data': [] } """ user_exist = get_user(username, without_fields=None) status = change_password.func_name + ' - ' try: generic_status_code = 0 vfense_status_code = 0 if user_exist: pass_strength = check_password(new_password) original_encrypted_password = user_exist[UserKeys.Password].encode('utf-8') original_password_verified = ( Crypto().verify_bcrypt_hash(password, original_encrypted_password) ) encrypted_new_password = Crypto().hash_bcrypt(new_password) new_password_verified_against_orignal_password = ( Crypto().verify_bcrypt_hash(new_password, original_encrypted_password) ) if (original_password_verified and pass_strength[0] and not new_password_verified_against_orignal_password): user_data = {UserKeys.Password: encrypted_new_password} object_status, _, _, _ = ( update_user(username, user_data) ) if object_status == DbCodes.Replaced: msg = 'Password changed for user %s - ' % (username) generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = UserCodes.PasswordChanged elif new_password_verified_against_orignal_password: msg = 'New password is the same as the original - user %s - ' % (username) object_status = DbCodes.Unchanged generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = UserFailureCodes.NewPasswordSameAsOld elif original_password_verified and not pass_strength[0]: msg = 'New password is to weak for user %s - ' % (username) object_status = DbCodes.Unchanged generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = UserFailureCodes.WeakPassword elif not original_password_verified: msg = 'Password not verified for user %s - ' % (username) object_status = DbCodes.Unchanged generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = UserFailureCodes.InvalidPassword results = { ApiResultKeys.DB_STATUS_CODE: object_status, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.UPDATED_IDS: [username], ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } else: msg = 'User %s does not exist - ' % (username) object_status = DbCodes.Skipped generic_status_code = GenericCodes.InvalidId vfense_status_code = UserFailureCodes.UserNameDoesNotExist results = { ApiResultKeys.DB_STATUS_CODE: object_status, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } except Exception as e: logger.exception(e) status_code = DbCodes.Errors generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = UserFailureCodes.FailedToUpdateUser msg = 'Failed to update password for user %s: %s' % (username, str(e)) results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return(results)