2016-04-03 20:03:58 +00:00
|
|
|
import re
|
|
|
|
from datetime import datetime
|
2016-04-06 15:12:40 +00:00
|
|
|
from sqlalchemy import func
|
2016-04-03 20:03:58 +00:00
|
|
|
from szurubooru import config, db, errors
|
|
|
|
from szurubooru.util import auth, misc
|
|
|
|
|
2016-04-08 17:46:59 +00:00
|
|
|
def create_user(session, name, password, email):
|
2016-04-03 20:03:58 +00:00
|
|
|
''' Create an user with given parameters and returns it. '''
|
|
|
|
user = db.User()
|
|
|
|
update_name(user, name)
|
|
|
|
update_password(user, password)
|
|
|
|
update_email(user, email)
|
2016-04-08 17:46:59 +00:00
|
|
|
if not session.query(db.User).count():
|
2016-04-09 19:39:28 +00:00
|
|
|
user.rank = 'admin'
|
2016-04-08 17:46:59 +00:00
|
|
|
else:
|
|
|
|
user.rank = config.config['default_rank']
|
2016-04-03 20:03:58 +00:00
|
|
|
user.creation_time = datetime.now()
|
|
|
|
user.avatar_style = db.User.AVATAR_GRAVATAR
|
|
|
|
return user
|
|
|
|
|
|
|
|
def update_name(user, name):
|
|
|
|
''' Validate and update user's name. '''
|
|
|
|
name = name.strip()
|
2016-04-06 18:38:45 +00:00
|
|
|
name_regex = config.config['user_name_regex']
|
2016-04-03 20:03:58 +00:00
|
|
|
if not re.match(name_regex, name):
|
|
|
|
raise errors.ValidationError(
|
|
|
|
'Name must satisfy regex %r.' % name_regex)
|
|
|
|
user.name = name
|
|
|
|
|
|
|
|
def update_password(user, password):
|
|
|
|
''' Validate and update user's password. '''
|
2016-04-06 18:38:45 +00:00
|
|
|
password_regex = config.config['password_regex']
|
2016-04-03 20:03:58 +00:00
|
|
|
if not re.match(password_regex, password):
|
|
|
|
raise errors.ValidationError(
|
|
|
|
'Password must satisfy regex %r.' % password_regex)
|
|
|
|
user.password_salt = auth.create_password()
|
|
|
|
user.password_hash = auth.get_password_hash(user.password_salt, password)
|
|
|
|
|
|
|
|
def update_email(user, email):
|
|
|
|
''' Validate and update user's email. '''
|
|
|
|
email = email.strip() or None
|
|
|
|
if not misc.is_valid_email(email):
|
|
|
|
raise errors.ValidationError(
|
|
|
|
'%r is not a vaild email address.' % email)
|
|
|
|
user.email = email
|
|
|
|
|
|
|
|
def update_rank(user, rank, authenticated_user):
|
|
|
|
rank = rank.strip()
|
2016-04-06 18:38:45 +00:00
|
|
|
available_ranks = config.config['ranks']
|
2016-04-06 17:16:44 +00:00
|
|
|
if not rank in available_ranks:
|
2016-04-03 20:03:58 +00:00
|
|
|
raise errors.ValidationError(
|
2016-04-06 18:38:45 +00:00
|
|
|
'Bad rank %r. Valid ranks: %r' % (rank, available_ranks))
|
2016-04-06 17:16:44 +00:00
|
|
|
if available_ranks.index(authenticated_user.rank) \
|
|
|
|
< available_ranks.index(rank):
|
|
|
|
raise errors.AuthError('Trying to set higher rank than your own')
|
|
|
|
user.rank = rank
|
2016-04-03 20:03:58 +00:00
|
|
|
|
|
|
|
def bump_login_time(user):
|
|
|
|
''' Update user's login time to current date. '''
|
|
|
|
user.last_login_time = datetime.now()
|
|
|
|
|
|
|
|
def reset_password(user):
|
|
|
|
''' Reset password for an user. '''
|
|
|
|
password = auth.create_password()
|
|
|
|
user.password_salt = auth.create_password()
|
|
|
|
user.password_hash = auth.get_password_hash(user.password_salt, password)
|
|
|
|
return password
|
|
|
|
|
|
|
|
def get_by_name(session, name):
|
|
|
|
''' Retrieve an user by its name. '''
|
2016-04-06 15:12:40 +00:00
|
|
|
return session.query(db.User) \
|
|
|
|
.filter(func.lower(db.User.name) == func.lower(name)) \
|
|
|
|
.first()
|
2016-04-06 15:56:34 +00:00
|
|
|
|
|
|
|
def get_by_name_or_email(session, name_or_email):
|
|
|
|
''' Retrieve an user by its name or email. '''
|
|
|
|
return session.query(db.User) \
|
|
|
|
.filter(
|
|
|
|
(func.lower(db.User.name) == func.lower(name_or_email))
|
|
|
|
| (func.lower(db.User.email) == func.lower(name_or_email))) \
|
|
|
|
.first()
|