def createsuperuser_command(username, email, password, no_input=False): if no_input: if username is None or password is None: sys.exit( 'Error: --username and --password are required for --noinput.') else: try: while True: username = input('Username: '******'Email address (optional): ').strip() if email and not utils.validate_email(email): print('Error: Enter a valid email address.') else: break while True: password = input('Password: '******'Password (again): ') if not len(password): print('Error: Blank passwords are not allowed.') elif password != repeat_password: print('Error: The passwords did not match.') else: break except KeyboardInterrupt: sys.exit('Cancelled.') errors = Users.create_super_user(username, password, email) if errors: for error in errors: print('Error: {}'.format(error)) exit(1) else: print('Superuser {} was created successfully.'.format(username))
def edit_user(username, new_username=None, new_email=False, new_is_superuser=None, new_projects=None): errors = [] if not Users.user_exists(username): errors.append('Username {} does not exist'.format(username)) else: user = Users.get_user_dictionary(username) # Cannot remove superuser permissions if there is only one superuser superusers = [u for u in Users.users() if u['is_superuser']] if user['is_superuser'] and not new_is_superuser and len( superusers) == 1: errors.append( 'Cannot remove Superuser permission for this user') if new_username is not None and new_username != username: new_username = new_username.strip() if len(new_username) == 0: errors.append('Username cannot be blank') elif Users.user_exists(new_username): errors.append( 'Username {} already exists'.format(new_username)) else: user['username'] = new_username if type(new_email) is str or new_email is None: valid_email = False if type(new_email) is str: new_email = new_email.strip() if len(new_email) == 0: new_email = None elif utils.validate_email(new_email): valid_email = True else: errors.append('{} is not a valid email address'.format( new_email)) if (new_email is None or valid_email) and new_email != user['email']: user['email'] = new_email if new_is_superuser is not None and new_is_superuser != user[ 'is_superuser']: user['is_superuser'] = new_is_superuser if new_projects is not None and new_projects != user['projects']: user['projects'] = new_projects if not errors: users = Users.users() users[:] = [u for u in users if u.get('username') != username] users.append(user) Users.set_users(users) Users.save() return errors
def create_user(username, password, email=None, is_superuser=False, projects=None): errors = [] projects = projects or {} if not Users.users_file_exists(): Users.create_users_file() username = username.strip() if len(username) == 0: errors.append('Username cannot be blank') elif Users.user_exists(username): errors.append('Username {} already exists'.format(username)) if email is not None: email = email.strip() if len(email) == 0: email = None elif not utils.validate_email(email): errors.append('{} is not a valid email address'.format(email)) if len(password) == 0: errors.append('Password cannot be blank') if not errors: id_ = str(uuid.uuid4())[:8] hashed_password = generate_password_hash(password) user = Users.generate_user_dictionary(id_, username, hashed_password, email, is_superuser, projects) Users.users().append(user) Users.save() return errors