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) if not user: return False password = mod_data.get(UserKey.Password) if password: password = password.encode('utf-8') 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: user.current_customer = current_customer default_customer = mod_data.get(UserKey.DefaultCustomer) if default_customer: customer = Hierarchy.get_customer(default_customer) if customer: user.default_customer = default_customer customers = mod_data.get(UserKey.Customers) if customers: for customer in customers: c = Hierarchy.get_customer(customer) if c: Hierarchy.toggle_user_from_customer( user, c, ) groups = mod_data.get(UserKey.Groups) if groups: customer_context = mod_data.get('customer_context') if customer_context: c = Hierarchy.get_customer(customer_context) else: c = Hierarchy.get_customer(user.current_customer) if c: for group in groups: g = Hierarchy.get_group(group) if g: Hierarchy.toggle_group_of_user( user, g, c ) if Hierarchy.save_user(user): return True return False
def create_user( user_name=None, full_name=None, email=None, password=None, groups=None, default_customer=None, customers=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 user_name ): return False, "Username/password is needed." try: if Hierarchy.get_user(user_name): return False, ( "Username `%s` already exist." % user_name ) # Get the Customer(s) that will be added to this user. customers_to_add = [] if customers: for customer_name in customers: c = Hierarchy.get_customer(customer_name) if c: customers_to_add.append(c) if default_customer: defult_cusomter = Hierarchy.get_customer(default_customer) add_customer = True if default_customer: for c in customer_to_add: if c.customer_name == dc.customer_name: add_customer = False break if add_customer: customers_to_add.append(default_cusotmer) else: if customers_to_add: default_customer = customers_to_add[0] else: default_customer = Hierarchy.get_customer(DefaultCustomer) customers_to_add.append(default_customer) #if not customers: # customers = [default_customer] #if added_default: # if DefaultCustomer not in customers: # customers.append(DefaultCustomer) # Now a Customer type. #default_customer = Hierarchy.get_customer(default_customer) #if not customers_to_add: # customers_to_add.append(default_customer) ############################################################# # Get the Group(s) that will be added to this user. groups_to_add = [] if groups: groups_list = [] for group_name in groups: g = Hierarchy.get_group( group_name, default_customer.customer_name ) if g: groups_list.append(g) groups_to_add.extend(groups_list) else: g = Hierarchy.get_group( DefaultGroup.ReadOnly, default_customer.customer_name ) if g: groups_to_add.append(g) ############################################################# user_name = user_name.strip() full_name = full_name.strip() if not password: password = generate_pass() password = Crypto.hash_bcrypt(password.encode('utf-8')) user = User( user_name, password, full_name, email, default_customer.customer_name, default_customer.customer_name ) saved = Hierarchy.save_user(user) if saved: for group in groups_to_add: Hierarchy.toggle_group_of_user( group=group, user=user, customer=default_customer ) for customer in customers_to_add: Hierarchy.toggle_user_from_customer( user=user, customer=customer ) return user, '' except Exception as e: logger.error("Unable to create user `%s`." % user_name) logger.exception(e) return None
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 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