Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
linkedin_url=linkedin_url, twitter_url=twitter_url,
facebook_url=facebook_url, google_url=google_url,
github_url=github_url, website_url=website_url, bio=bio,
is_want_news=is_want_news)
# Creating default team with the same name as the user
# user is admin of his/her own team
team = Team(name=name, admin=user)
session.add(team)
session.add(user)
try:
session.commit()
except IntegrityError as e:
session.rollback()
message = ''
if select_user_by_name(session, name) is not None:
message += 'username is already in use'
elif select_team_by_name(session, name) is not None:
# We only check for team names if username is not in db
message += 'username is already in use as a team name'
if select_user_by_email(session, lower_case_email) is not None:
if message:
message += ' and '
message += 'email is already in use'
if message:
raise NameClashError(message)
else:
raise e
logger.info('Creating {}'.format(user))
logger.info('Creating {}'.format(team))
return user
def approve_user(session, name):
"""Approve a user once it is created.
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
name : str
The name of the user.
"""
user = select_user_by_name(session, name)
if user.access_level == 'asked':
user.access_level = 'user'
user.is_authenticated = True
session.commit()
def make_user_admin(session, name):
"""Make a user a RAMP admin.
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
name : str
The name of the user.
"""
user = select_user_by_name(session, name)
user.access_level = 'admin'
user.is_authenticated = True
session.commit()
def is_admin(session, event_name, user_name):
"""Whether or not a user is administrator or administrate an event.
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
event_name : str
The event name.
user_name : str
The user name.
"""
event = select_event_by_name(session, event_name)
user = select_user_by_name(session, user_name)
if user.access_level == 'admin':
return True
event_admin = select_event_admin_by_instance(session, event, user)
if event_admin is None:
return False
return True
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
name : str or None
The name of the user. If None, all users will be queried.
Returns
-------
user : :class:`ramp_database.model.User` or list of \
:class:`ramp_database.model.User`
The queried user.
"""
return (select_user_by_email(session, name) or
select_user_by_name(session, name))
def is_accessible_event(session, event_name, user_name):
"""Whether or not an event is public or and a user is registered to RAMP
or and admin.
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
event_name : str
The event name.
user_name : str
The user name.
"""
event = select_event_by_name(session, event_name)
user = select_user_by_name(session, user_name)
if event is None:
return False
if user.access_level == 'asked':
return False
if event.is_public or is_admin(session, event_name, user_name):
return True
return False
Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to directly perform the operation on the database.
event_name : str
The event name.
user_name : str
The user name.
Returns
-------
event_admin : :class:`ramp_database.model.EventAdmin` or None
The event/admin instance queried.
"""
event = select_event_by_name(session, event_name)
user = select_user_by_name(session, user_name)
return select_event_admin_by_instance(session, event, user)