Add route for viewing user Crafty permissions

The route includes the permission mask, counters and limits
This commit is contained in:
luukas 2022-05-25 00:55:49 +03:00
parent 6dadce2c9a
commit 57ef5e27dd
No known key found for this signature in database
GPG Key ID: CC4915E8D71FC044
3 changed files with 90 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import logging
import typing as t
from enum import Enum
from peewee import (
ForeignKeyField,
@ -99,7 +100,7 @@ class PermissionsCrafty:
try:
user_crafty = UserCrafty.get(UserCrafty.user_id == user_id)
except DoesNotExist:
user_crafty = UserCrafty.insert(
UserCrafty.insert(
{
UserCrafty.user_id: user_id,
UserCrafty.permissions: "000",
@ -114,6 +115,13 @@ class PermissionsCrafty:
user_crafty = PermissionsCrafty.get_user_crafty(user_id)
return user_crafty
@staticmethod
def get_user_crafty_optional(user_id) -> t.Optional[UserCrafty]:
try:
return UserCrafty.get(UserCrafty.user_id == user_id)
except DoesNotExist:
return None
@staticmethod
def add_user_crafty(user_id, uc_permissions):
user_crafty = UserCrafty.insert(

View File

@ -25,6 +25,9 @@ from app.classes.web.routes.api.servers.server.stats import ApiServersServerStat
from app.classes.web.routes.api.servers.server.users import ApiServersServerUsersHandler
from app.classes.web.routes.api.users.index import ApiUsersIndexHandler
from app.classes.web.routes.api.users.user.index import ApiUsersUserIndexHandler
from app.classes.web.routes.api.users.user.permissions import (
ApiUsersUserPermissionsHandler,
)
from app.classes.web.routes.api.users.user.pfp import ApiUsersUserPfpHandler
from app.classes.web.routes.api.users.user.public import ApiUsersUserPublicHandler
@ -63,6 +66,11 @@ def api_handlers(handler_args):
ApiUsersUserPfpHandler,
handler_args,
),
(
r"/api/v2/users/(@me)/permissions/?",
ApiUsersUserPermissionsHandler,
handler_args,
),
(
r"/api/v2/users/(@me)/pfp/?",
ApiUsersUserPfpHandler,

View File

@ -0,0 +1,73 @@
import logging
import typing as t
from app.classes.models.crafty_permissions import (
EnumPermissionsCrafty,
PermissionsCrafty,
)
from app.classes.web.base_api_handler import BaseApiHandler
logger = logging.getLogger(__name__)
SERVER_CREATION: t.Final[str] = EnumPermissionsCrafty.SERVER_CREATION.name
USER_CONFIG: t.Final[str] = EnumPermissionsCrafty.USER_CONFIG.name
ROLES_CONFIG: t.Final[str] = EnumPermissionsCrafty.ROLES_CONFIG.name
class ApiUsersUserPermissionsHandler(BaseApiHandler):
def get(self, user_id: str):
auth_data = self.authenticate_user()
if not auth_data:
return
(
_,
exec_user_crafty_permissions,
_,
_,
user,
) = auth_data
if user_id in ["@me", user["user_id"]]:
user_id = user["user_id"]
res_data = PermissionsCrafty.get_user_crafty(user_id)
elif EnumPermissionsCrafty.USER_CONFIG not in exec_user_crafty_permissions:
return self.finish_json(
400,
{
"status": "error",
"error": "NOT_AUTHORIZED",
},
)
else:
# has User_Config permission and isn't viewing self
res_data = PermissionsCrafty.get_user_crafty_optional(user_id)
if res_data is None:
return self.finish_json(
404,
{
"status": "error",
"error": "USER_NOT_FOUND",
},
)
self.finish_json(
200,
{
"status": "ok",
"data": {
"permissions": res_data.permissions,
"counters": {
SERVER_CREATION: res_data.created_server,
USER_CONFIG: res_data.created_user,
ROLES_CONFIG: res_data.created_role,
},
"limits": {
SERVER_CREATION: res_data.limit_server_creation,
USER_CONFIG: res_data.limit_user_creation,
ROLES_CONFIG: res_data.limit_role_creation,
},
},
},
)