server/general: cosmetic changes
This commit is contained in:
parent
5053598a13
commit
2af8c9c1a4
4 changed files with 3 additions and 18 deletions
|
@ -30,14 +30,11 @@ def _serialize_user(authenticated_user, user):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
class UserListApi(BaseApi):
|
class UserListApi(BaseApi):
|
||||||
''' API for lists of users. '''
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._search_executor = search.SearchExecutor(search.UserSearchConfig())
|
self._search_executor = search.SearchExecutor(search.UserSearchConfig())
|
||||||
|
|
||||||
def get(self, context):
|
def get(self, context):
|
||||||
''' Retrieve a list of users. '''
|
|
||||||
auth.verify_privilege(context.user, 'users:list')
|
auth.verify_privilege(context.user, 'users:list')
|
||||||
query = context.get_param_as_string('query')
|
query = context.get_param_as_string('query')
|
||||||
page = context.get_param_as_int('page', 1)
|
page = context.get_param_as_int('page', 1)
|
||||||
|
@ -51,7 +48,6 @@ class UserListApi(BaseApi):
|
||||||
}
|
}
|
||||||
|
|
||||||
def post(self, context):
|
def post(self, context):
|
||||||
''' Create a new user. '''
|
|
||||||
auth.verify_privilege(context.user, 'users:create')
|
auth.verify_privilege(context.user, 'users:create')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -69,10 +65,7 @@ class UserListApi(BaseApi):
|
||||||
return {'user': _serialize_user(context.user, user)}
|
return {'user': _serialize_user(context.user, user)}
|
||||||
|
|
||||||
class UserDetailApi(BaseApi):
|
class UserDetailApi(BaseApi):
|
||||||
''' API for individual users. '''
|
|
||||||
|
|
||||||
def get(self, context, user_name):
|
def get(self, context, user_name):
|
||||||
''' Retrieve an user. '''
|
|
||||||
auth.verify_privilege(context.user, 'users:view')
|
auth.verify_privilege(context.user, 'users:view')
|
||||||
user = users.get_by_name(context.session, user_name)
|
user = users.get_by_name(context.session, user_name)
|
||||||
if not user:
|
if not user:
|
||||||
|
@ -80,7 +73,6 @@ class UserDetailApi(BaseApi):
|
||||||
return {'user': _serialize_user(context.user, user)}
|
return {'user': _serialize_user(context.user, user)}
|
||||||
|
|
||||||
def put(self, context, user_name):
|
def put(self, context, user_name):
|
||||||
''' Update an existing user. '''
|
|
||||||
user = users.get_by_name(context.session, user_name)
|
user = users.get_by_name(context.session, user_name)
|
||||||
if not user:
|
if not user:
|
||||||
raise errors.NotFoundError('User %r not found.' % user_name)
|
raise errors.NotFoundError('User %r not found.' % user_name)
|
||||||
|
@ -120,7 +112,6 @@ class UserDetailApi(BaseApi):
|
||||||
return {'user': _serialize_user(context.user, user)}
|
return {'user': _serialize_user(context.user, user)}
|
||||||
|
|
||||||
def delete(self, context, user_name):
|
def delete(self, context, user_name):
|
||||||
''' Delete an existing user. '''
|
|
||||||
user = users.get_by_name(context.session, user_name)
|
user = users.get_by_name(context.session, user_name)
|
||||||
if not user:
|
if not user:
|
||||||
raise errors.NotFoundError('User %r not found.' % user_name)
|
raise errors.NotFoundError('User %r not found.' % user_name)
|
||||||
|
|
|
@ -1,4 +1,2 @@
|
||||||
''' Base model for every database resource. '''
|
|
||||||
|
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
Base = declarative_base() # pylint: disable=invalid-name
|
Base = declarative_base() # pylint: disable=invalid-name
|
||||||
|
|
|
@ -3,7 +3,7 @@ import datetime
|
||||||
import json
|
import json
|
||||||
import falcon
|
import falcon
|
||||||
|
|
||||||
def json_serial(obj):
|
def json_serializer(obj):
|
||||||
''' JSON serializer for objects not serializable by default JSON code '''
|
''' JSON serializer for objects not serializable by default JSON code '''
|
||||||
if isinstance(obj, datetime.datetime):
|
if isinstance(obj, datetime.datetime):
|
||||||
serial = obj.isoformat()
|
serial = obj.isoformat()
|
||||||
|
@ -52,4 +52,4 @@ class JsonTranslator(object):
|
||||||
if 'result' not in request.context:
|
if 'result' not in request.context:
|
||||||
return
|
return
|
||||||
response.body = json.dumps(
|
response.body = json.dumps(
|
||||||
request.context.result, default=json_serial, indent=2)
|
request.context.result, default=json_serializer, indent=2)
|
||||||
|
|
|
@ -20,7 +20,6 @@ def get_legacy_password_hash(salt, password):
|
||||||
return digest.hexdigest()
|
return digest.hexdigest()
|
||||||
|
|
||||||
def create_password():
|
def create_password():
|
||||||
''' Create an easy-to-remember password. '''
|
|
||||||
alphabet = {
|
alphabet = {
|
||||||
'c': list('bcdfghijklmnpqrstvwxyz'),
|
'c': list('bcdfghijklmnpqrstvwxyz'),
|
||||||
'v': list('aeiou'),
|
'v': list('aeiou'),
|
||||||
|
@ -30,7 +29,6 @@ def create_password():
|
||||||
return ''.join(random.choice(alphabet[l]) for l in list(pattern))
|
return ''.join(random.choice(alphabet[l]) for l in list(pattern))
|
||||||
|
|
||||||
def is_valid_password(user, password):
|
def is_valid_password(user, password):
|
||||||
''' Return whether the given password for a given user is valid. '''
|
|
||||||
salt, valid_hash = user.password_salt, user.password_hash
|
salt, valid_hash = user.password_salt, user.password_hash
|
||||||
possible_hashes = [
|
possible_hashes = [
|
||||||
get_password_hash(salt, password),
|
get_password_hash(salt, password),
|
||||||
|
@ -39,9 +37,7 @@ def is_valid_password(user, password):
|
||||||
return valid_hash in possible_hashes
|
return valid_hash in possible_hashes
|
||||||
|
|
||||||
def verify_privilege(user, privilege_name):
|
def verify_privilege(user, privilege_name):
|
||||||
'''
|
''' Throw an AuthError if the given user doesn't have given privilege. '''
|
||||||
Throw an AuthError if the given user doesn't have given privilege.
|
|
||||||
'''
|
|
||||||
all_ranks = config.config['ranks']
|
all_ranks = config.config['ranks']
|
||||||
|
|
||||||
assert privilege_name in config.config['privileges']
|
assert privilege_name in config.config['privileges']
|
||||||
|
|
Loading…
Reference in a new issue