def delete_customer(name=None): """Delete a Customer for good. Args: name: Name of the customer to delete. Returns: True if customer was deleted, False otherwise. """ if not name: return False, "Customer name was not provided." if name == DefaultCustomer: return False, ( "Can not delete the `%s` customer." % DefaultCustomer ) error_msg = "Unable to delete customer `%s`." % name try: if not Hierarchy.get_customer(name): return False, 'Did not find customer `%s`' % name deleted = actions.db_delete_by_secondary( collection=Collection.Groups, values=name, index=GroupKey.CustomerId ) if not deleted: msg = 'Unable to delete groups from customer.' logger.error(msg) deleted = actions.db_delete_by_secondary( collection=Collection.UsersPerCustomer, values=name, index=UsersPerCustomerKey.CustomerId ) if not deleted: msg = 'Unable to delete users from customer.' logger.error(msg) deleted = actions.db_delete( collection=Collection.Customers, _id=name ) if not deleted: logger.error(error_msg) return False, error_msg Hierarchy._users_of_delete_customer(name) return True, '' except Exception as e: logger.error(error_msg) logger.exception(e) return False, error_msg
def delete_group(group_name=None, customer_name=None): """Delete a Group for good. Returns: True if group was deleted, False otherwise. """ if ( not group_name and not customer_name ): return False, "Group and customer name is needed." if group_name in SafeGroups: return False, "Can not delete the `%s` group." % group_name error_msg = ( "Unable to delete group `%s` from customer `%s`" % (group_name, customer_name) ) try: if not Hierarchy.get_group(group_name, customer_name): return False, ( 'Did not find group `%s` for customer `%s`' % (group_name, customer_name) ) deleted = actions.db_delete_by_secondary( collection=Collection.GroupsPerUser, values=[ group_name, customer_name ], index=GroupsPerUserKey.GroupUserAndCustomerId ) if not deleted: msg = 'Nothing was deleted from group per user.' logger.error(msg) deleted = actions.db_delete_by_secondary( collection=Collection.Groups, values=[ group_name, customer_name ], index=GroupKey.GroupNameAndCustomerId ) if not deleted: msg = 'Unable to delete group `%s`' % group_name logger.error(msg) return False, msg else: return True, '' except Exception as e: logger.error(error_msg) logger.exception(e) logger.error(error_msg) return False, error_msg
def delete_user(name=None): """Delete a User for good. Args: name: Name of the user to delete. Returns: True if user was deleted, False otherwise. """ if name == AdminUser: return False, 'Cannot delete the %s user.' % AdminUser if not name: return False, 'Username was not provided.' try: user = Hierarchy.get_user(name) if not user: return False, 'Did not find user `%s`' % name deleted = actions.db_delete_by_secondary( collection=Collection.UsersPerCustomer, values=name, index=UsersPerCustomerKey.UserId ) if not deleted: msg = 'Unable to delete users per customer.' logger.error(msg) deleted = actions.db_delete_by_secondary( collection=Collection.GroupsPerUser, values=name, index=GroupsPerUserKey.UserId ) if not deleted: msg = 'Unable to delete group per user.' logger.error(msg) deleted = actions.db_delete( collection=Collection.Users, _id=name ) if not deleted: msg = 'Unable to delete user `%s`' % name logger.error(msg) return False, msg return True, '' except Exception as e: logger.error("Unable to delete user `%s`" % name) logger.exception(e) return False, "Unable to delete user `%s`" % name
def toggle_group_from_customer(group=None, customer=None, both=False): """Toggles the group for the customer. If the group is part of customer then it's removed. If the group is not part of the customer then it's added. Changes are not saved to the DB. Args: group: A Group instance. customer: A Customer instance. both: Whether to toggle both Group and Customer instances or just customer. Returns: True if successfully toggled, False otherwise. """ try: if ( not group or not customer ): return result result = actions.db_get_by_secondary( collection=Collection.Groups, values=[ group.group_name, customer.customer_name ], index=GroupKey.GroupNameAndCustomerId ) if len(result) >= 1: result = actions.db_delete_by_secondary( collection=Collection.Groups, values=[ group.group_name, customer.customer_name ], index=GroupKey.GroupNameAndCustomerId ) else: res = Hierarchy.save_group_per_customer(group, customer) if res: result = True except Exception as e: result = False logger.error( "Uable to toggle group `%s` from customer `%s`" % (group, customer) ) logger.exception(e) return result
def toggle_user_from_customer(user=None, customer=None): """Toggles the user for the customer. If the user is part of customer then it's removed. If the user is not part of the customer then it's added. Changes are not saved to the DB. Args: user: A User instance. customer: A Customer instance. Returns: True if successfully toggled, False otherwise. """ result = False try: if ( not user or not customer ): return result result = actions.db_get_by_secondary( collection=Collection.UsersPerCustomer, values=[ user.user_name, customer.customer_name ], index=UsersPerCustomerKey.UserAndCustomerId ) if len(result) >= 1: result = actions.db_delete_by_secondary( collection=Collection.UsersPerCustomer, values=[ user.user_name, customer.customer_name ], index=UsersPerCustomerKey.UserAndCustomerId ) ### SAFETY HACK ### Make sure a user has at least one customer. customer_count = actions.db_get_by_secondary( collection=Collection.UsersPerCustomer, values=user.user_name, index=UsersPerCustomerKey.UserId ) if len(customer_count) <= 0: def_customer = Hierarchy.get_customer(DefaultCustomer) res = Hierarchy.save_user_per_customer(user, def_customer) if res: result = True else: res = Hierarchy.save_user_per_customer(user, customer) if res: result = True except Exception as e: logger.error( "Uable to toggle user `%s` from customer `%s`" % (user, customer) ) logger.exception(e) return result
def toggle_group_of_user(group=None, user=None, customer=None): """Toggles the user for the group for a particular customer. If the user is part of group then it's removed. If the user is not part of the group then it's added. Args: user: A User instance. group: A Group instance. customer: A Customer instance. Returns: True if successfully toggled, False otherwise. """ result = False try: if ( not group or not user or not customer ): return result result = actions.db_get_by_secondary( collection=Collection.GroupsPerUser, values=[ group.group_name, user.user_name, customer.customer_name ], index=GroupsPerUserKey.GroupUserAndCustomerId ) if len(result) >= 1: result = actions.db_delete_by_secondary( collection=Collection.GroupsPerUser, values=[ group.group_name, user.user_name, customer.customer_name ], index=GroupsPerUserKey.GroupUserAndCustomerId ) else: res = Hierarchy.save_group_per_user(group, user, customer) if res: result = True return result except Exception as e: logger.error( "Unable to toggle user `%s` from group `%s`." % (user, group) ) logger.exception(e) return False