forked from Infra/risotto
remove public information in message, it's remplace by role
This commit is contained in:
@ -89,8 +89,7 @@ async def handle(request):
|
||||
async def api(request, risotto_context):
|
||||
global tiramisu
|
||||
if not tiramisu:
|
||||
config = await Config(get_messages(load_shortarg=True,
|
||||
only_public=True)[1])
|
||||
config = await Config(get_messages(load_shortarg=True)[1])
|
||||
await config.property.read_write()
|
||||
tiramisu = await config.option.dict(remotable='none')
|
||||
return tiramisu
|
||||
@ -113,11 +112,8 @@ async def get_app(loop):
|
||||
print(_('======== Registered messages ========'))
|
||||
for message in messages:
|
||||
web_message = f'/api/{version}/{message}'
|
||||
if dispatcher.messages[version][message]['public']:
|
||||
print(f' - {web_message}')
|
||||
else:
|
||||
pattern = dispatcher.messages[version][message]['pattern']
|
||||
print(f' - {web_message} (private {pattern})')
|
||||
pattern = dispatcher.messages[version][message]['pattern']
|
||||
print(f' - {web_message} ({pattern})')
|
||||
routes.append(post(web_message, handle))
|
||||
print()
|
||||
print(_('======== Registered extra routes ========'))
|
||||
|
@ -44,7 +44,6 @@ class MessageDefinition:
|
||||
'uri',
|
||||
'description',
|
||||
'parameters',
|
||||
'public',
|
||||
'errors',
|
||||
'pattern',
|
||||
'related',
|
||||
@ -54,7 +53,6 @@ class MessageDefinition:
|
||||
# default value for non mandatory key
|
||||
self.version = u''
|
||||
self.parameters = OrderedDict()
|
||||
self.public = False
|
||||
self.errors = []
|
||||
self.related = []
|
||||
self.response = None
|
||||
@ -63,10 +61,7 @@ class MessageDefinition:
|
||||
for key, value in raw_def.items():
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
if key == 'public':
|
||||
if not isinstance(value, bool):
|
||||
raise ValueError(_("{} must be a boolean, not {}").format(key, value))
|
||||
elif key == 'pattern':
|
||||
if key == 'pattern':
|
||||
if value not in ['rpc', 'event', 'error']:
|
||||
raise Exception(_('unknown pattern {}').format(value))
|
||||
elif key == 'parameters':
|
||||
@ -86,9 +81,6 @@ class MessageDefinition:
|
||||
getattr(self, key)
|
||||
except AttributeError:
|
||||
raise Exception(_('mandatory key not set {} message').format(key))
|
||||
# message with pattern = error must be public
|
||||
if self.public is False and self.pattern == 'error':
|
||||
raise Exception(_('Error message must be public : {}').format(self.uri))
|
||||
if self.uri != message:
|
||||
raise Exception(_(f'yaml file name "{message}.yml" does not match uri "{self.uri}"'))
|
||||
|
||||
@ -581,7 +573,7 @@ def _get_root_option(select_option, optiondescriptions):
|
||||
return OptionDescription('root', 'root', options_obj)
|
||||
|
||||
|
||||
def get_messages(load_shortarg=False, only_public=False):
|
||||
def get_messages(load_shortarg=False):
|
||||
"""generate description from yml files
|
||||
"""
|
||||
optiondescriptions = OrderedDict()
|
||||
@ -592,8 +584,7 @@ def get_messages(load_shortarg=False, only_public=False):
|
||||
messages.sort()
|
||||
for message_name in messages:
|
||||
message_def = get_message(message_name)
|
||||
if message_def.pattern not in ['rpc', 'event'] or \
|
||||
(not message_def.public and only_public):
|
||||
if message_def.pattern not in ['rpc', 'event']:
|
||||
continue
|
||||
optiondescriptions_name.append(message_def.uri)
|
||||
optiondescriptions_name.sort()
|
||||
@ -603,11 +594,9 @@ def get_messages(load_shortarg=False, only_public=False):
|
||||
properties=frozenset(['mandatory', 'positional']))
|
||||
for message_name in messages:
|
||||
message_def = get_message(message_name)
|
||||
if message_def.pattern not in ['rpc', 'event'] or \
|
||||
(not message_def.public and only_public):
|
||||
if message_def.pattern not in ['rpc', 'event']:
|
||||
continue
|
||||
optiondescriptions_info[message_def.uri] = {'pattern': message_def.pattern,
|
||||
'public': message_def.public}
|
||||
optiondescriptions_info[message_def.uri] = {'pattern': message_def.pattern}
|
||||
if message_def.pattern == 'rpc':
|
||||
optiondescriptions_info[message_def.uri]['response'] = _parse_responses(message_def,
|
||||
message_name)
|
||||
|
1
src/risotto/services/uri/__init__.py
Normal file
1
src/risotto/services/uri/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .uri import Risotto
|
43
src/risotto/services/uri/uri.py
Normal file
43
src/risotto/services/uri/uri.py
Normal file
@ -0,0 +1,43 @@
|
||||
from typing import Dict, List
|
||||
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
@register('v1.uri.role.join', None, database=True)
|
||||
async def uri_role_join(self,
|
||||
risotto_context: Context,
|
||||
role_name: str,
|
||||
uri: str) -> Dict:
|
||||
# Verify if user exists and get ID
|
||||
sql = '''
|
||||
SELECT URIId
|
||||
FROM URI
|
||||
WHERE URIName = $1
|
||||
'''
|
||||
uri_id = await risotto_context.connection.fetchval(sql,
|
||||
uri)
|
||||
if uri_id is None:
|
||||
raise Exception(_(f'unable to find message {uri}'))
|
||||
sql = '''
|
||||
INSERT INTO RoleURI(RoleName, URIId)
|
||||
VALUES ($1,$2)
|
||||
'''
|
||||
uri_id = await risotto_context.connection.fetchrow(sql,
|
||||
role_name,
|
||||
uri_id)
|
||||
return {'role_name': role_name,
|
||||
'uri': uri}
|
||||
|
||||
@register('v1.uri.role.list', None, database=True)
|
||||
async def uri_role_list(self,
|
||||
risotto_context: Context) -> List[Dict]:
|
||||
sql = '''
|
||||
SELECT RoleName as role_name, URI.URIName as uri
|
||||
FROM RoleURI, URI
|
||||
WHERE RoleURI.URIId = URI.URIId
|
||||
'''
|
||||
return [dict(r) for r in await risotto_context.connection.fetch(sql)]
|
1
src/risotto/services/user/__init__.py
Normal file
1
src/risotto/services/user/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .user import Risotto
|
143
src/risotto/services/user/user.py
Normal file
143
src/risotto/services/user/user.py
Normal file
@ -0,0 +1,143 @@
|
||||
from typing import Dict, Optional
|
||||
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
@register('v1.user.create', None, database=True)
|
||||
async def user_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
user_name: str,
|
||||
user_surname: str) -> Dict:
|
||||
user_insert = """INSERT INTO RisottoUser(UserLogin, UserName, UserSurname)
|
||||
VALUES ($1,$2,$3)
|
||||
RETURNING UserId
|
||||
"""
|
||||
user_id = await risotto_context.connection.fetchval(user_insert,
|
||||
user_login,
|
||||
user_name,
|
||||
user_surname)
|
||||
return {'user_id': user_id,
|
||||
'user_login': user_login,
|
||||
'user_name': user_name,
|
||||
'user_surname': user_surname}
|
||||
|
||||
@register('v1.user.list', None, database=True)
|
||||
async def user_list(self,
|
||||
risotto_context: Context) -> Dict:
|
||||
sql = '''
|
||||
SELECT UserId as user_id, UserLogin as user_login, UserName as user_name, UserSurname as user_surname
|
||||
FROM RisottoUser
|
||||
'''
|
||||
users = await risotto_context.connection.fetch(sql)
|
||||
return [dict(r) for r in users]
|
||||
|
||||
@register('v1.user.delete', None, database=True)
|
||||
async def user_delete(self,
|
||||
risotto_context: Context,
|
||||
user_login: str) -> Dict:
|
||||
sql = '''
|
||||
DELETE FROM RisottoUser
|
||||
WHERE UserLogin = $1
|
||||
RETURNING UserId as user_id, UserLogin as user_login, UserName as user_name, UserSurname as user_surname
|
||||
'''
|
||||
user = await risotto_context.connection.fetchrow(sql,
|
||||
user_login)
|
||||
if user is None:
|
||||
raise Exception(_(f'unable to find user {user_login}'))
|
||||
return dict(user)
|
||||
|
||||
@register('v1.user.role.create', None, database=True)
|
||||
async def user_role_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
role_name: str,
|
||||
role_attribute: str,
|
||||
role_attribute_value: str) -> Dict:
|
||||
# Verify if user exists and get ID
|
||||
sql = '''
|
||||
SELECT UserId
|
||||
FROM RisottoUser
|
||||
WHERE UserLogin = $1
|
||||
'''
|
||||
user_id = await risotto_context.connection.fetchval(sql,
|
||||
user_login)
|
||||
if user_id is None:
|
||||
raise Exception(_(f'unable to find user {user_login}'))
|
||||
sql = '''INSERT INTO UserRole(RoleUserId, RoleName, RoleAttribute, RoleAttributeValue)
|
||||
VALUES($1,$2,$3,$4)
|
||||
RETURNING RoleId
|
||||
'''
|
||||
role_id = await risotto_context.connection.fetchval(sql,
|
||||
user_id,
|
||||
role_name,
|
||||
role_attribute,
|
||||
role_attribute_value)
|
||||
return {'role_id': role_id,
|
||||
'user_login': user_login,
|
||||
'role_name': role_name,
|
||||
'role_attribute': role_attribute,
|
||||
'role_attribute_value': role_attribute_value}
|
||||
|
||||
@register('v1.user.role.list', None, database=True)
|
||||
async def user_role_list(self,
|
||||
risotto_context: Context,
|
||||
user_login: Optional[str]) -> Dict:
|
||||
if not user_login:
|
||||
sql = '''
|
||||
SELECT RoleId as role_id, RoleName as role_name, RoleAttribute as role_attribute, RoleAttributeValue as role_attribute_value, RisottoUser.UserLogin as user_login
|
||||
FROM UserRole, RisottoUser
|
||||
WHERE UserRole.RoleUserId = RisottoUser.UserId
|
||||
'''
|
||||
roles = await risotto_context.connection.fetch(sql)
|
||||
else:
|
||||
# Verify if user exists and get ID
|
||||
sql = '''
|
||||
SELECT UserId
|
||||
FROM RisottoUser
|
||||
WHERE UserLogin = $1
|
||||
'''
|
||||
user_id = await risotto_context.connection.fetchval(sql,
|
||||
user_login)
|
||||
if user_id is None:
|
||||
raise Exception(_(f'unable to find user {user_login}'))
|
||||
sql = '''
|
||||
SELECT RoleId as role_id, RoleName as role_name, RoleAttribute as role_attribute, RoleAttributeValue as role_attribute_value, RisottoUser.UserLogin as user_login
|
||||
FROM UserRole, RisottoUser
|
||||
WHERE UserRole.RoleUserId = RisottoUser.UserId AND UserRole.RoleUserId = $1
|
||||
'''
|
||||
roles = await risotto_context.connection.fetch(sql,
|
||||
user_id)
|
||||
return [dict(r) for r in roles]
|
||||
#
|
||||
# FIXME comment savoir quel role il faut supprimer ? avec attribut ou juste l'ID ?
|
||||
# @register('v1.user.role.delete', None, database=True)
|
||||
# async def user_role_delete(self,
|
||||
# risotto_context: Context,
|
||||
# user_login: str,
|
||||
# role_name: str) -> Dict:
|
||||
# # Verify if user exists and get ID
|
||||
# sql = '''
|
||||
# SELECT UserId
|
||||
# FROM RisottoUser
|
||||
# WHERE UserLogin = $1
|
||||
# '''
|
||||
# user_id = await risotto_context.connection.fetchval(sql,
|
||||
# user_login)
|
||||
# if user_id is None:
|
||||
# raise Exception(_(f'unable to find user {user_login}'))
|
||||
# sql = '''
|
||||
# DELETE FROM RisottoRole
|
||||
# WHERE RoleName = $1 AND UserId = $2
|
||||
# RETURNING RoleId as role_id, RoleName as role_name, RoleAttribute as role_attribute, RoleAttributeValue as role_attribute_value
|
||||
# '''
|
||||
# role = await risotto_context.connection.fetchrow(sql,
|
||||
# role_name,
|
||||
# user_id)
|
||||
# if role is None:
|
||||
# raise Exception(_(f'unable to find role {role_name}'))
|
||||
# return dict(role)
|
Reference in New Issue
Block a user