def register(request, username=None, password=None): if db.get_user(request.db_session, username): raise BadRequest(request, error_code=ErrorCodes.Validation.UserAlreadyExists, args=username) return add_user(request, email=username, password=password)
def is_member_of_group(request, username, group_id): is_member = False if username is not None and group_id is not None: user = db.get_user(request.db_session, username) if db.get_group_to_user(request.db_session, group_id, user.id) is not None: is_member = True return is_member
def get_user_profile(request, email=None): """ Get user details required to show in profile page. Generate auth_token and return. Return None if no email is provided. :param db_session: Database session. :param email: Email of the user. Default value is None. :return: User object and auth_token """ user = db.get_user(request.db_session, email) if email is not None else None auth_token = 'Basic %s' % base64.b64encode('{0}:{1}'.format( user.email, user.password)) if user is not None else None return user, auth_token
def add_group_member(request, group_id, member_id=None, member_email=None): if member_id is not None: user = db.get_user_by_id(request.db_session, id=member_id) elif member_email is not None: user = db.get_user(request.db_session, email=member_email) else: raise Exception("Required Parameter is missing!") if user is not None: if db.get_group_to_user(request.db_session, group_id, user.id): raise BadRequest(request, error_code=ErrorCodes.Validation.UserAlreadyExists, args=user.email) return db.add_group_to_user(request.db_session, group_id=group_id, user_id=user.id) else: raise Exception("User does not exit.")
def add_group_member(request, group_id, member_id=None, member_email=None): if member_id is not None: user = db.get_user_by_id(request.db_session, id=member_id) elif member_email is not None: user = db.get_user(request.db_session, email=member_email) else: raise Exception("Required Parameter is missing!") if user is not None: if db.get_group_to_user(request.db_session, group_id, user.id): raise BadRequest( request, error_code=ErrorCodes.Validation.UserAlreadyExists, args=user.email) return db.add_group_to_user(request.db_session, group_id=group_id, user_id=user.id) else: raise Exception("User does not exit.")
def authenticate_user(request, username=None, password=None): # check the username/password and return a User user = db.get_user(request.db_session, username) if user: hashed_pw = user.password #check password. if the password matches, return a #User object with associated information if bcrypt.hashpw(password, hashed_pw) == hashed_pw: return { 'id': user.id, 'user_id': user.email, 'password': user.password, 'admin': user.is_admin } else: return None
def get_user(request, email): return db.get_user(request.db_session, email) if email is not None else None