Exemple #1
0
def register(request):
    form = forms.accounts.Register(request.POST)

    if len(request.POST) > 0 and form.validate():
        password = form.data.get('password')
    else:
        return dict(form=form)

    user = models.User(**form.data)
    user.set_password(password)
    role = models.Role.objects(name='anonymous').first()
    user.roles.append(role)
    user.ip_address = request.environ['REMOTE_ADDR']
    user.save()

    return HTTPFound(location=request.route_path('index'))
Exemple #2
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)

    models.initial(settings)

    role_names = [
        'admin', 'lecturer', 'staff', 'moderator', 'member', 'anonymous'
    ]

    for rname in role_names:
        role = models.Role.objects(name=rname).first()
        if not role:
            role = models.Role(name=rname)
            role.save()
            print('add role:', rname)

    adminuser = '******'
    adminpass = '******'

    admin = models.User.objects(username=adminuser).first()
    sm = crypto.SecretManager(settings.get('pumbaa.secret'))
    if admin is None:
        admin = models.User(username=adminuser)
        admin.first_name = 'Administrator'
        admin.last_name = 'CoE'
        admin.display_name = 'Administrator'
        admin.status = 'activate'
        admin.email = '*****@*****.**'
        admin.password = sm.get_hash_password(adminpass)
        admin.roles.append(models.Role.objects(name='admin').first())
        admin.save()

        print('add admin user:', adminuser)
Exemple #3
0
def online_login_complete(request):
    context = request.context

    domain = context.profile["accounts"][0]['domain']
    user_id = context.profile["accounts"][0]['userid']

    user = None
    user = models.User.objects(online_profiles__domain=domain,\
                                online_profiles__user_id=str(user_id)).first()
    if 'verifiedEmail' in context.profile:
        email = context.profile['verifiedEmail']
    else:
        email = context.profile['preferredUsername'] + "@" + domain

    if 'name' in context.profile:
        first_name = context.profile['name'].get('givenName', None)
        last_name = context.profile['name'].get('familyName', None)
        if first_name is None:
            if 'formatted' in context.profile['name']:
                first_name = context.profile['name']['formatted']
                last_name = 'No Last Name'
    else:
        names = context.profile['displayName'].split(' ')
        first_name = names[0]
        last_name = names[-1]

    new_user = False
    if not user:
        new_user = True

        user = models.User.objects(email=email).first()
        if user:
            return Response(
                'This Email is available on system, please contact administrator for registration'
            )

        user = models.User()
        profile = models.Profile()
        profile.user_id = user_id
        profile.domain = domain

        user.first_name = first_name
        user.last_name = last_name
        user.email = email
        user.display_name = context.profile['displayName']
        user.online_profiles.append(profile)
        user.default_profile = domain
        user.roles.append(models.Role.objects(name="anonymous").first())

        check_display_name = models.User.objects(
            username=context.profile['preferredUsername']).first()
        if not check_display_name:
            user.username = context.profile['preferredUsername']
        else:
            user.username = context.profile['preferredUsername'] + "_"

    profile = user.get_profile(domain)

    profile.first_name = first_name
    profile.last_name = last_name

    profile.display_name = context.profile['displayName']

    if 'profile.php=' in context.profile['preferredUsername']:
        profile.username = profile.username.split('=')[-1]
    else:
        profile.username = context.profile['preferredUsername']

    profile.email = email
    profile.profile_source = context.profile

    # support old user accounts
    if user.display_name is None:
        user.display_name = user.username

    user.save()
    user.reload()
    headers = remember(request, str(user.id))

    if user.status == 'wait for approval':
        new_user = True

    if new_user:
        return HTTPFound(location=request.route_path('home'), headers=headers)

    session = request.session
    location = request.route_path('home')

    if "came_from" in session:
        if request.route_path('login') not in session['came_from']:
            location = session['came_from']
        del session['came_from']

    return HTTPFound(location=location, headers=headers)