Coverage for auth/lib_right.py: 46%
178 statements
« prev ^ index » next coverage.py v7.9.1, created at 2026-01-26 23:58 +0100
« prev ^ index » next coverage.py v7.9.1, created at 2026-01-26 23:58 +0100
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3__author__ = 'moilerat'
4__copyright__ = "Safia copyright 2022-2025 VR"
5__license__ = "LICENSETOBEDEFINED : on premise treatment custom"
6"""lib_right.py Get and Grant logical right on object"""
7__credits__ = [""]
8from lib.lib_version import version_name
9__version__ = version_name
10__maintainer__ = "moilerat and Safia Team"
11__email__ = "safia.dev@rubbia.fr"
12__status__ = "Alpha Dev See version_name"
16class LibRight():
17 # TODO VR 11-7 : j'aurai préféré faire avec un heritage plutot qu'une composition, mmmm
18 # On peut mettre ici l'intelligence des noms de table ! grrr
19 # Mais ou met-on les owner etcatera ?
20 # TODO duplicate Parce qu'on veut un LibRightData et puis la version sans DB ?
21 # et on pourrait pas noter lib_internal_data plutot lib_connect ? mmm
22 def __init__(self, lib_internal_data):
23 self.lib_internal_data = lib_internal_data
25 self.table_name_group_datou = "mtrright.group_datou"
26 self.table_name_group_project = "mtrright.group_project"
27# self.table_name_group_project = "mtruser.group_project"
28# self.table_name_datou = "mtrdatou.datou" # Et voila, ca c'est dupliquer, clairement on doit fusionner parce qu'on ne peut pas aovir les droits d'un cote et les datous de l'autre sauf si on fait des requete juste à partir du datou_id, a oui, c'est le cas !
29# Et si ! on en a besoin si on est owner
30 # TODO VR on pourrait appeler toutes les colonnes object_id meme si ce ne sont pas les memes type d'objet avec des FK différentes
31 self.table_name_datou = "mtrdatou.datou"
33 def create_self_group(self, user_id):
34 print("TODO create self group")
35 res = self.lib_internal_data.create_group(user_id, "self_group")
36 if res == None:
37 print(" We expect the test to be null !")
38 else :
39 group_id = res["id"]
40 self.lib_internal_data.add_role_to_user_group(group_id, user_id, "admin")
42 # On veut gérer dans l'interface les roles differents ?
43 def get_all_group(self, role = None):
44 pass
46 # TODO VR 11-7 : renommer "right" en role ?
47 def get_all_project(self, role = None):
48 pass
50 # On veut gérer dans l'interface les roles differents ?
51 def get_all_datou(self, role = None):
52 pass
54 # pas forcément utile
55 def get_role_on_group(self, user_id, group_id):
57 ret = self.lib_internal_data.is_owner(self.lib_internal_data.groups_table, user_id, group_id)
58 if ret:
59 return "owner"
61 data = self.lib_internal_data.get_role_on_object(self.table_name_group_project, user_id, group_id)
63 higher_role = None
64 for d in data:
65 if d["role"] == "admin":
66 return "admin"
67 elif d["role"] == "write":
68 higher_role = "write"
69 elif d["role"] == "read":
70 if higher_role == None:
71 higher_role = "read"
73 return higher_role
74 def get_group(self, user_id, role = "owner"):
75 list_group = []
76 if role == "owner":
77 list_group = self.lib_internal_data.get_group_by_owner(user_id)
78 else :
79 print("get group not owned not yet implemented !")
80 list_group = []
81 list_group_ids = list(map(lambda x : x["id"], list_group))
82 list_group_with_users = self.lib_internal_data.get_all_member_groups(list_group_ids)
83 map_group_id_group = {g["id"] : [] for g in list_group}
84 for g in list_group_with_users:
85 map_group_id_group[g["group_id"]].append(g)
87 for g in list_group:
88 g["users"] = map_group_id_group[g["id"]]
89 return list_group
93 def create_group(self, user_id, name):
94 return self.lib_internal_data.create_group(user_id, name)
96 def get_role_on_project(self, user_id, project_id):
98 ret = self.lib_internal_data.is_owner(self.lib_internal_data.projects_table, user_id, project_id)
99 if ret:
100 return "owner"
102 data = self.lib_internal_data.get_role_on_object(self.table_name_group_project, user_id, project_id)
104 higher_role = None
105 for d in data:
106 if d["role"] == "admin":
107 return "admin"
108 elif d["role"] == "write":
109 higher_role = "write"
110 elif d["role"] == "read":
111 if higher_role == None:
112 higher_role = "read"
114 return higher_role
116 def get_role_on_datou(self, user_id, project_id):
117 pass
119 # TODO and others !
120 def save_role_to_datou(self):
121 pass
123 # TODO and others !
124 def delete_role_on_datou(self):
125 pass
127 def get_projects_with_access(self, user_id):
128 list_ids = self.lib_internal_data.get_projects_from_owner(user_id)
129 list_ids_int = list(map(lambda x : x["id"], list_ids))
131 list_project_with_role = list(map(lambda x : {"id" : x["id"], "role" : "owner"}, list_ids))
132# return list_ids
134 list_ids_with_role_non_admin = self.lib_internal_data.get_role_on_object(self.table_name_group_project, user_id)
136# return list_project_with_role + list_ids_with_role
138 for l in list_ids_with_role_non_admin:
139 if l["object_id"] not in list_ids_int:
140 l["id"] = l["object_id"]
141 list_project_with_role.append(l)
143 return list_project_with_role
145 def get_all_role_access_to_project(self, project_id):
146 # - [ ] get_owner, or omitted
147 list_use_ids_with_role_on_project = self.lib_internal_data.get_all_role_on_object(self.table_name_group_project, project_id)
149 return list_use_ids_with_role_on_project
151 def get_email_from_user_ids(self, list_user_ids):
152 if len(list_user_ids) == 0:
153 return {}
155 list_user_ids_email = self.lib_internal_data.get_email_from_user_ids(list_user_ids)
156 map_user_id_email = {d["id"] : d["mail"] for d in list_user_ids_email}
158 return map_user_id_email
160 def is_project_admin(self, user_id, project_id):
161 list_ids_owner = self.lib_internal_data.get_projects_from_owner(user_id, project_id)
162 if len(list_ids_owner) > 0 and int(list_ids_owner[0]["id"]) == int(project_id):
163 return True
164 list_ids_with_role_non_admin = self.lib_internal_data.get_role_on_object(self.table_name_group_project, user_id, project_id)
165 if len(list_ids_with_role_non_admin) >= 1 and list_ids_with_role_non_admin[0]["role"] == "admin":
166 if len(list_ids_with_role_non_admin) == 1:
167 print("INTERNAL ERROR treated as warning (strange ? not that much but to audit) before was contourner : Current bug on 17-7-23")
168 return True
169 else :
170 return False
172 def is_group_admin(self, user_id, group_id):
173 if group_id == None:
174 print("Internal error group_id none")
175 return False
176 list_ids_owner = self.lib_internal_data.get_group_by_owner(user_id) #, group_id)
177 # if len(list_ids_owner) > 0 and int(list_ids_owner[0]["id"]) == int(group_id):
178 for l in list_ids_owner:
179 if int(l["id"]) == int(group_id):
180 return True
182 list_all_member_groups = self.lib_internal_data.get_all_member_groups([group_id])
183 list_all_member_groups_ids = list(map(lambda x : x["user_id"], list_all_member_groups))
184 if user_id in list_all_member_groups_ids:
185 return True
186 else:
187 return False
189 def is_user_admin_config(self, user_id):
190 from server.safia import lcs
191 group_admin = lcs.get_admin_group_id()
192 if not str(group_admin).isdigit():
193 print("Internal error group_admin none")
194 return False
195 return self.is_group_admin(user_id, group_admin)
197 def create_invitation(self, user_id, project_id, role, mail, host,
198 action = "grant_project_role"):
199 from lib.lib_util import get_unique_id
200 token_uuid = get_unique_id()
201 token = str(token_uuid)
202 # TODO check role is read, write or admin
203 link = host + "invitation/" + token
204 param_json = {"role" : role, "data_id" : project_id, "token" : token}
206 recorded = self.lib_internal_data.create_invitation(user_id, mail, action, param_json, link, token)
207 if len(recorded) > 0:
208 return {"link" : link, "token" : token}
209 else :
210 return {}
212 def get_invitation_info_from_token(self, token):
213 all_info = self.lib_internal_data.get_invitation_info_from_token(token)
214 return all_info
216 def get_self_group_id(self, user_id):
217 all_group = self.lib_internal_data.get_group_by_owner(user_id, "self_group")
218 if len(all_group) == 1:
219 return all_group[0]["id"]
220 else :
221 return None
223 def add_role_project_to_user(self, user_id, project_id, role):
224 group_id = self.get_self_group_id(user_id)
225 if group_id == None:
226 print("Internal error we could create the group or audit")
227 else :
228 self.lib_internal_data.add_role_project_to_user(group_id, project_id, role)
230 # TODO rename with group
231 def remove_role_to_user(self, user_id, project_id):
232 group_id = self.get_self_group_id(user_id)
233 if group_id == None:
234 print("Internal error we could create the group or audit")
235 else :
236 self.lib_internal_data.remove_role_to_user(group_id, project_id)
238 def add_role_group_to_user(self, user_id, group_id, role):
239 if group_id == None:
240 print("Internal error we could create the group or audit")
241 else :
242 self.lib_internal_data.add_role_to_user_group(group_id, user_id, role)
244 def remove_role_group_to_user(self, user_id, group_id):
245 self.lib_internal_data.remove_role_group_to_user(self, user_id, group_id)
247 def update_invitation_used(self, id, user_id):
248 self.lib_internal_data.update_invitation_used(id, user_id)
252# TODO VR 21-9-23 It should encapsulate all the access to token, their creation and when getting them from external services
253class CheckToken():
254 # TODO VR 21-9-23 Comme d'hab on fait par heritage plutot que par composition
255 # TODO VR 21-9-23 C'est en complémentarité mais conccurence avec les trucs d'avant
256 def __init__(self, lib_data_int = None, lib_at_ext = None):
257 self.lib_data_int = lib_data_int
258 self.lib_at_ext = lib_at_ext
260 def check_otp(self, token):
261 pass
263 # update
264 def save_otp(self, token, disable = None, new_expiry_date = None):
265 # mtruser.connexion_otp
266 pass
268 def create_otp(self):
269 pass
271 def check_otp_from_local(self):
272 pass