def edit_customer(name, mod_data=None): """Edit customer properties. Args: name: Name of the customer. mod_data: A dic of GroupKeys as the key with the new values. Returns: True if successful, False otherwise. """ if not name and not mod_data: return False customer = Hierarchy.get_customer(name) net_throttle = mod_data.get(CustomerKey.NetThrottle) if net_throttle: customer.net_throttle = net_throttle cpu_throttle = mod_data.get(CustomerKey.CpuThrottle) if cpu_throttle: customer.cpu_throttle = cpu_throttle groups = mod_data.get(CustomerKey.Groups) if groups: for group in groups: g = Hierarchy.get_group(group) if g: g, customer = Hierarchy.toggle_group_from_customer( g, customer, both=True) _db.save_group(g) users = mod_data.get(CustomerKey.Users) if users: for user in users: u = Hierarchy.get_user(user) if u: u, customer = Hierarchy.toggle_user_from_customer( u, customer, both=True) _db.save_user(u) return _db.save_customer(customer)
def save_user(user=None): if user: return _db.save_user(user)
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 or name == 'default' ): return False customer = Hierarchy.get_customer(name) # Build users and groups list before deleting customer. customer_groups = customer.get_groups() found_groups = [] for group in customer_groups: g = Hierarchy.get_group({GroupKey.Id: group.id}) if g: found_groups.append(g) customer_users = customer.get_users() found_users = [] for user in customer_users: u = Hierarchy.get_user(user.name) if u: found_users.append(u) deleted = _db._db_delete(collection_name=CustomerCollection, _id=customer.name) if deleted: for group in found_groups: group, __ = Hierarchy.toggle_group_from_customer( group, customer, both=True ) _db.save_group(group) for user in found_users: user, __ = Hierarchy.toggle_user_from_customer( user, customer, both=True ) _db.save_user(user) return deleted
def edit_customer(name, mod_data=None): """Edit customer properties. Args: name: Name of the customer. mod_data: A dic of GroupKeys as the key with the new values. Returns: True if successful, False otherwise. """ if not name and not mod_data: return False customer = Hierarchy.get_customer(name) net_throttle = mod_data.get(CustomerKey.NetThrottle) if net_throttle: customer.net_throttle = net_throttle cpu_throttle = mod_data.get(CustomerKey.CpuThrottle) if cpu_throttle: customer.cpu_throttle = cpu_throttle groups = mod_data.get(CustomerKey.Groups) if groups: for group in groups: g = Hierarchy.get_group(group) if g: g, customer = Hierarchy.toggle_group_from_customer( g, customer, both=True) _db.save_group(g) users = mod_data.get(CustomerKey.Users) if users: for user in users: u = Hierarchy.get_user(user) if u: u, customer = Hierarchy.toggle_user_from_customer( u, customer, both=True ) _db.save_user(u) return _db.save_customer(customer)
def edit_user(user=None, mod_data=None): """Edit user properties. Args: user: Name of the user. mod_data: A dic of UserKeys as the key with the new values. Returns: True if successful, False otherwise. """ if not user and not mod_data: return False user = Hierarchy.get_user(user) password = mod_data.get(UserKey.Password) if password: user.password = Crypto.hash_bcrypt(password) full_name = mod_data.get(UserKey.FullName) if full_name: user.full_name = full_name email = mod_data.get(UserKey.Email) if email: user.email = email current_customer = mod_data.get(UserKey.CurrentCustomer) if current_customer: customer = Hierarchy.get_customer(current_customer) if customer: customer_name = '' current_customer = user.get_current_customer() if current_customer: customer_name = current_customer.name if not customer.name == customer_name: user.set_current_customer(customer) default_customer = mod_data.get(UserKey.DefaultCustomer) if default_customer: customer = Hierarchy.get_customer(default_customer) if customer: user.set_current_customer(customer) customers = mod_data.get(UserKey.Customers) if customers: for customer in customers: c = Hierarchy.get_customer(customer) if c: user, c = Hierarchy.toggle_user_from_customer( user, c, both=True ) _db.save_customer(c) groups = mod_data.get(UserKey.Groups) if groups: for group in groups: g = Hierarchy.get_group(group) if g: user, g = Hierarchy.toggle_user_from_group(user, g, both=True) _db.save_group(g) if _db.save_user(user): return True return False
def create_user(name=None, full_name=None, email=None, password=None, groups=None, customers=None, default_customer=None): """Create a new User and save it. All parameters are required *except* groups and customers. Args: name: Name of the user. full_name: Full name of the user (ie First and last name). email: User's email address. password: User's plain text password. groups: A list of dicts consisting of either an id key or name key describing the group. customers: Customers this user should be added to. List of customer names. default_customer: The default customer for this user. Will be the first data available to the user. Returns: The newly created User if added successfully, None otherwise. """ if ( not name or not password ): return False # Get the Group instances that will be added to this user. if groups: groups_list = [] for group in groups: g = Hierarchy.get_group(group) if g: groups_list.append(g) groups = groups_list else: groups = [] g = Hierarchy.get_group({GroupKey.Name: 'Read Only'}) if g: groups.append(g) # Get the Customer instances that will be added to this user. if customers: customers_list = [] for customer in customers: c = Hierarchy.get_customer(customer) if c: customers_list.append(c) if customers_list: customers = customers_list else: customers = [Hierarchy.get_customer(DefaultCustomer)] else: customers = [Hierarchy.get_customer(DefaultCustomer)] if default_customer: default_customer = Hierarchy.get_customer(default_customer) else: default_customer = customers[0] name = name.strip() full_name = full_name.strip() password = Crypto.hash_bcrypt(password) user = User(name, full_name, email, password, groups, customers, default_customer=default_customer, current_customer=default_customer) _id = _db.save_user(user) if _id == '': user.id = user.name for g in groups: _, mod_group = Hierarchy.toggle_user_from_group(user, g) _db.save_group(mod_group) for c in customers: _, mod_customer = Hierarchy.toggle_user_from_customer(user, c) _db.save_customer(mod_customer) return user return None
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 or name == 'default'): return False customer = Hierarchy.get_customer(name) # Build users and groups list before deleting customer. customer_groups = customer.get_groups() found_groups = [] for group in customer_groups: g = Hierarchy.get_group({GroupKey.Id: group.id}) if g: found_groups.append(g) customer_users = customer.get_users() found_users = [] for user in customer_users: u = Hierarchy.get_user(user.name) if u: found_users.append(u) deleted = _db._db_delete(collection_name=CustomerCollection, _id=customer.name) if deleted: for group in found_groups: group, __ = Hierarchy.toggle_group_from_customer(group, customer, both=True) _db.save_group(group) for user in found_users: user, __ = Hierarchy.toggle_user_from_customer(user, customer, both=True) _db.save_user(user) return deleted
def edit_user(user=None, mod_data=None): """Edit user properties. Args: user: Name of the user. mod_data: A dic of UserKeys as the key with the new values. Returns: True if successful, False otherwise. """ if not user and not mod_data: return False user = Hierarchy.get_user(user) password = mod_data.get(UserKey.Password) if password: user.password = Crypto.hash_bcrypt(password) full_name = mod_data.get(UserKey.FullName) if full_name: user.full_name = full_name email = mod_data.get(UserKey.Email) if email: user.email = email current_customer = mod_data.get(UserKey.CurrentCustomer) if current_customer: customer = Hierarchy.get_customer(current_customer) if customer: customer_name = '' current_customer = user.get_current_customer() if current_customer: customer_name = current_customer.name if not customer.name == customer_name: user.set_current_customer(customer) default_customer = mod_data.get(UserKey.DefaultCustomer) if default_customer: customer = Hierarchy.get_customer(default_customer) if customer: user.set_current_customer(customer) customers = mod_data.get(UserKey.Customers) if customers: for customer in customers: c = Hierarchy.get_customer(customer) if c: user, c = Hierarchy.toggle_user_from_customer(user, c, both=True) _db.save_customer(c) groups = mod_data.get(UserKey.Groups) if groups: for group in groups: g = Hierarchy.get_group(group) if g: user, g = Hierarchy.toggle_user_from_group(user, g, both=True) _db.save_group(g) if _db.save_user(user): return True return False
def create_user(name=None, full_name=None, email=None, password=None, groups=None, customers=None, default_customer=None): """Create a new User and save it. All parameters are required *except* groups and customers. Args: name: Name of the user. full_name: Full name of the user (ie First and last name). email: User's email address. password: User's plain text password. groups: A list of dicts consisting of either an id key or name key describing the group. customers: Customers this user should be added to. List of customer names. default_customer: The default customer for this user. Will be the first data available to the user. Returns: The newly created User if added successfully, None otherwise. """ if (not name or not password): return False # Get the Group instances that will be added to this user. if groups: groups_list = [] for group in groups: g = Hierarchy.get_group(group) if g: groups_list.append(g) groups = groups_list else: groups = [] g = Hierarchy.get_group({GroupKey.Name: 'Read Only'}) if g: groups.append(g) # Get the Customer instances that will be added to this user. if customers: customers_list = [] for customer in customers: c = Hierarchy.get_customer(customer) if c: customers_list.append(c) if customers_list: customers = customers_list else: customers = [Hierarchy.get_customer(DefaultCustomer)] else: customers = [Hierarchy.get_customer(DefaultCustomer)] if default_customer: default_customer = Hierarchy.get_customer(default_customer) else: default_customer = customers[0] name = name.strip() full_name = full_name.strip() password = Crypto.hash_bcrypt(password) user = User(name, full_name, email, password, groups, customers, default_customer=default_customer, current_customer=default_customer) _id = _db.save_user(user) if _id == '': user.id = user.name for g in groups: _, mod_group = Hierarchy.toggle_user_from_group(user, g) _db.save_group(mod_group) for c in customers: _, mod_customer = Hierarchy.toggle_user_from_customer(user, c) _db.save_customer(mod_customer) return user return None