2021-05-20 01:21:18 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2021 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License")
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2022-10-24 16:40:21 +03:00
|
|
|
from google.cloud import ndb # type: ignore
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
from framework import basehandlers
|
|
|
|
from framework import permissions
|
2022-08-18 04:14:19 +03:00
|
|
|
from internals import user_models
|
|
|
|
|
|
|
|
|
|
|
|
def user_to_json_dict(user):
|
|
|
|
return {
|
|
|
|
'is_admin': user.is_admin,
|
|
|
|
'is_site_editor': user.is_site_editor,
|
|
|
|
'email': user.email,
|
|
|
|
'id': user.key.integer_id(),
|
|
|
|
}
|
|
|
|
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
class AccountsAPI(basehandlers.APIHandler):
|
|
|
|
"""User accounts store info on registered users."""
|
|
|
|
|
|
|
|
# TODO(jrobbins): do_get
|
|
|
|
|
|
|
|
@permissions.require_admin_site
|
2022-10-16 09:39:35 +03:00
|
|
|
def do_post(self, **kwargs):
|
2021-05-20 01:21:18 +03:00
|
|
|
"""Process a request to create an account."""
|
|
|
|
email = self.get_param('email', required=True)
|
|
|
|
is_admin = self.get_bool_param('isAdmin')
|
2022-07-14 00:31:27 +03:00
|
|
|
is_site_editor = self.get_bool_param('isSiteEditor')
|
|
|
|
user = self.create_account(email, is_admin, is_site_editor)
|
2022-08-18 04:14:19 +03:00
|
|
|
response_json = user_to_json_dict(user)
|
2021-05-20 01:21:18 +03:00
|
|
|
return response_json
|
|
|
|
|
2022-07-14 00:31:27 +03:00
|
|
|
def create_account(self, email, is_admin, is_site_editor):
|
2021-05-20 01:21:18 +03:00
|
|
|
"""Create and store a new account entity."""
|
|
|
|
# Don't add a duplicate email address.
|
2022-08-18 04:14:19 +03:00
|
|
|
user = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == email).get(keys_only=True)
|
2021-05-20 01:21:18 +03:00
|
|
|
if not user:
|
2022-08-18 04:14:19 +03:00
|
|
|
user = user_models.AppUser(email=str(email))
|
2021-05-20 01:21:18 +03:00
|
|
|
user.is_admin = is_admin
|
2022-07-14 00:31:27 +03:00
|
|
|
user.is_site_editor = is_site_editor
|
2021-05-20 01:21:18 +03:00
|
|
|
user.put()
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
self.abort(400, 'User already exists')
|
|
|
|
|
|
|
|
# TODO(jrobbins): do_patch
|
|
|
|
|
|
|
|
@permissions.require_admin_site
|
2022-10-16 09:39:35 +03:00
|
|
|
def do_delete(self, **kwargs):
|
2021-05-20 01:21:18 +03:00
|
|
|
"""Process a request to delete the specified account."""
|
2022-10-16 09:39:35 +03:00
|
|
|
account_id = kwargs.get('account_id', None)
|
2021-05-20 01:21:18 +03:00
|
|
|
if account_id:
|
|
|
|
self.delete_account(account_id)
|
|
|
|
return {'message': 'Done'}
|
|
|
|
else:
|
|
|
|
self.abort(400, msg='Account ID not specified')
|
|
|
|
|
|
|
|
def delete_account(self, account_id):
|
|
|
|
"""Delete the specified account."""
|
|
|
|
if account_id:
|
2022-08-18 04:14:19 +03:00
|
|
|
found_user = user_models.AppUser.get_by_id(int(account_id))
|
2021-05-20 01:21:18 +03:00
|
|
|
if found_user:
|
2022-09-12 23:56:50 +03:00
|
|
|
found_user.delete()
|
2021-05-20 01:21:18 +03:00
|
|
|
else:
|
|
|
|
self.abort(404, msg='Specified account ID not found')
|