def approve_users(): """Approve new user to log-in and sign-up to events.""" if not flask_login.current_user.access_level == 'admin': return redirect_to_user( 'Sorry {}, you do not have admin rights'.format( flask_login.current_user.firstname), is_error=True) if request.method == 'GET': # TODO: replace by some get_functions asked_users = User.query.filter_by(access_level='asked').all() asked_sign_up = EventTeam.query.filter_by(approved=False).all() return render_template('approve.html', asked_users=asked_users, asked_sign_up=asked_sign_up, admin=True) elif request.method == 'POST': users_to_be_approved = request.form.getlist('approve_users') event_teams_to_be_approved = request.form.getlist( 'approve_event_teams') message = "{}d users:\n".format(request.form["submit_button"][:-1]) for asked_user in users_to_be_approved: user = select_user_by_name(db.session, asked_user) if request.form["submit_button"] == "Approve!": approve_user(db.session, asked_user) subject = 'Your RAMP account has been approved' body = ('{}, your account has been approved. You can now ' 'sign-up for any open RAMP event.'.format(user.name)) send_mail(to=user.email, subject=subject, body=body) elif request.form["submit_button"] == "Remove!": delete_user(db.session, asked_user) message += "{}\n".format(asked_user) message += "{}d event_team:\n".format( request.form["submit_button"][:-1]) for asked_id in event_teams_to_be_approved: asked_event_team = EventTeam.query.get(asked_id) user = select_user_by_name(db.session, asked_event_team.team.name) if request.form["submit_button"] == "Approve!": sign_up_team(db.session, asked_event_team.event.name, asked_event_team.team.name) subject = ('Signed up for the RAMP event {}'.format( asked_event_team.event.name)) body = ('{}, you have been registered to the RAMP event {}. ' 'You can now proceed to your sandbox and make ' 'submissions.\nHave fun!!!'.format( user.name, asked_event_team.event.name)) send_mail(to=user.email, subject=subject, body=body) elif request.form["submit_button"] == "Remove!": delete_event_team(db.session, asked_event_team.event.name, asked_event_team.team.name) message += "{}\n".format(asked_event_team) return redirect_to_user(message, is_error=False, category="{}d users".format( request.form["submit_button"][:-1]))
def test_approve_user(session_scope_function): add_user(session_scope_function, name='test_user', password='******', lastname='Test', firstname='User', email='*****@*****.**', access_level='asked') user = get_user_by_name(session_scope_function, 'test_user') assert user.access_level == 'asked' assert user.is_authenticated is False approve_user(session_scope_function, 'test_user') user = get_user_by_name(session_scope_function, 'test_user') assert user.access_level == 'user' assert user.is_authenticated is True
def approve_single_user(user_name): """Approve a single user. This is usually used to approve user through email.""" if not flask_login.current_user.access_level == 'admin': return redirect_to_user( u'Sorry {}, you do not have admin rights'.format( flask_login.current_user.firstname), is_error=True) user = User.query.filter_by(name=user_name).one_or_none() if not user: return redirect_to_user(u'No user {}'.format(user_name), is_error=True) approve_user(db.session, user.name) return redirect_to_user(u'{} is signed up'.format(user), is_error=False, category='Successful sign-up')
def test_is_accessible_code(session_toy_db): # create a third user add_user( session_toy_db, name='test_user_3', password='******', lastname='Test_3', firstname='User_3', email='*****@*****.**', access_level='user') approve_user(session_toy_db, 'test_user_3') event_name = 'iris_test' sign_up_team(session_toy_db, event_name, 'test_user_3') # simulate a user which is not authenticated user = get_user_by_name(session_toy_db, 'test_user_2') user.is_authenticated = False assert not is_accessible_code(session_toy_db, event_name, user.name) # simulate a user which authenticated and author of the submission to a # public event user.is_authenticated = True assert is_accessible_code(session_toy_db, event_name, user.name) # simulate an admin user user = get_user_by_name(session_toy_db, 'test_iris_admin') user.is_authenticated = True assert is_accessible_code(session_toy_db, event_name, 'test_iris_admin') # simulate a user which is not signed up to the event user = add_user(session_toy_db, 'xx', 'xx', 'xx', 'xx', 'xx', 'user') user.is_authenticated = True assert not is_accessible_code(session_toy_db, event_name, user.name) # simulate that the event is not publicly opened event = get_event(session_toy_db, event_name) past_public_opening = event.public_opening_timestamp tomorrow = datetime.datetime.utcnow() + datetime.timedelta(days=1) event.public_opening_timestamp = tomorrow session_toy_db.commit() assert is_accessible_code(session_toy_db, event_name, 'test_user_3') # Make a submission submission_name = 'random_forest_10_10' ramp_config = generate_ramp_config(read_config(ramp_config_template())) path_submission = os.path.join( os.path.dirname(ramp_config['ramp_sandbox_dir']), submission_name ) sub = add_submission( session_toy_db, event_name, 'test_user_3', submission_name, path_submission ) # check that the user submitting the submission could access it assert is_accessible_code( session_toy_db, event_name, 'test_user_3', sub.id ) # change the admin of the team from ramp_database.model import Team, User team = (session_toy_db.query(Team) .filter(Team.name == 'test_user_3') .first()) user = (session_toy_db.query(User) .filter(User.name == 'test_user_2') .first()) team.admin_id = user.id team.admin = user session_toy_db.commit() # check that the admin can access the submission assert is_accessible_code( session_toy_db, event_name, 'test_user_2', sub.id ) # but others cannot assert not is_accessible_code( session_toy_db, event_name, 'test_user_3', sub.id ) event.public_opening_timestamp = past_public_opening session_toy_db.commit()