Пример #1
0
def facebook_authorized(resp):
    if resp is None:
        return "Access denied: reason=%s error=%s" % (
                request.args["error_reason"],
                request.args["error_description"]
            )
    session['oauth_token'] = (resp['access_token'], '')
    session['expires'] = resp['expires_in']
    print("Token expires in " + str(resp['expires_in']))

    # me = facebook_oauth.get("/me")
    # return str(me.data)

    me = facebook_oauth.get('/me?fields=id,name,first_name,last_name,email,picture')
    userID = me.data['id']
    userName = me.data['name']
    accessToken = resp['access_token']

    # If user exists in collection, logs them in
    # Otherwise, registers new user and logs them in
    # TODO get email if we can
    fb_user = user_utils.get_user(userID)
    if not fb_user:
        user_utils.add_user(userID, userName, me.data['first_name'], me.data['last_name'])
        user = User(userID)
        login_user(user)
        return "Successfully registered new user!"
    else:
        users.update_user(userID)
        user = User(userID, fb_user['account']['is_active'], fb_user['account']['is_admin'])
        login_user(user)
        return "Successfully logged in with Facebook!"
Пример #2
0
def google_authorized(resp):
    next = request.args.get('next')
    if resp is None:
        return "Access denied: reason=%s error=%s" % (
                request.args["error_reason"],
                request.args["error_description"]
            )
    session['oauth_token'] = (resp['access_token'], '')
    session['expires'] = resp['expires_in']
    print("Token expires in " + str(resp['expires_in']))

    me = google_oauth.get('userinfo')
    print(me.data)

    userID = me.data['id']
    userName = me.data['name'].title()
    accessToken = resp['access_token']
    email = me.data['email']

    domain = email.split('@')[1]
    if domain != 'ucla.edu' and domain != 'g.ucla.edu':
        return "Invalid email. UCLA email required."

    # If user exists in collection, logs them in
    # Otherwise, registers new user and logs them in
    # TODO get email if we can
    g_user = user_utils.get_user(userID)
    if not g_user:
        # Successfully registered new user
        user_utils.add_user(userID, userName, me.data['given_name'].title(), me.data['family_name'].title(), me.data['email'])
        user = User(userID)
        login_user(user)
        return "Successfully registered new user" if next == None else redirect(next)
    else:
        # Successfully logged in
        users.update_user(userID)
        user = User(userID, g_user['account']['is_active'], g_user['account']['is_admin'])
        login_user(user)
        return "Successfully logged in" if next == None else redirect(next)
Пример #3
0
def add_user_through_api():
    """
    :Route: POST /?user_id=int&full_name=Katrina A. Wijaya&first_name=Katrina&last_name=Wijaya&[email protected]&active=false&admin=true&password=str&username=str

    :Description: Add a new user with required id `user_id` and any optional fields that are set as query parameters.

    :param user_id: The int ID of a specific user
    :type user_id: int

    :param full_name: An optional query component/parameter to update the user's full name.
    :type full_name: str or None

    :param first_name: An optional query component/parameter to update the user's first name. Does not modify full name stored in database.
    :type first_name: str or None

    :param last_name: An optional query component/parameter to update the user's last name. Does not modify full name stored in database.
    :type last_name: str or None

    :param email: An optional query component/parameter to update the user's email. TODO: email verification.
    :type email: str or None

    :param active: An optional query component/parameter to update whether or not a user is active. If true, user has an activated account that they can log in to, otherwise account will be rejected/suspended from use
    :type active: boolean or None

    :param admin: An optional query component/parameter to update whether or not a user has admin permissions. All admins have same permissions so maybe should create a super admin.
    :type admin: boolean or None

    :param password: An optional query component/parameter to update the password for a user. TODO: actually supporting passwords/salting/hashing.
    :type password: str or None

    :param username: An optional query component/parameter to update the username for a user. TODO: actually supporting usernames.
    :type username: str or None

    :return: Success/error message

    :Requires: Admin permissions

    """
    user_id = request.args.get('id')
    full_name = request.args.get('full_name', '')
    first_name = request.args.get('first_name', '')
    last_name = request.args.get('last_name', '')
    email = request.args.get('email', '')
    active = request.args.get('active')
    admin = request.args.get('admin')
    password = request.args.get('password', '')
    username = request.args.get('username', '')

    if not user_id:
        # TODO: add ID automatically, don't require it to be supplied
        return "User ID required to add a new user"

    if active and active.lower() == "false":
        active = False
    else:
        active = True

    if admin and admin.lower() == "true":
        admin = True
    else:
        admin = False

    return user_utils.add_user(user_id, full_name, first_name, last_name,
                               email, active, admin, password, username)