Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
webomagic-auth
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Pratik Shrivastava
webomagic-auth
Commits
0bf10239
Commit
0bf10239
authored
Jul 23, 2024
by
deepdey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add role-web-api map api
parent
7ccdc95c
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
97 additions
and
4 deletions
+97
-4
app/routes/role/__init__.py
app/routes/role/__init__.py
+29
-0
app/routes/role/role_dao_service.py
app/routes/role/role_dao_service.py
+37
-1
app/routes/role/role_payload.py
app/routes/role/role_payload.py
+10
-0
app/routes/role/role_response.py
app/routes/role/role_response.py
+8
-0
app/routes/role/role_validator.py
app/routes/role/role_validator.py
+5
-0
app/routes/user/__init__.py
app/routes/user/__init__.py
+1
-1
app/routes/user/user_dao_service.py
app/routes/user/user_dao_service.py
+7
-2
No files found.
app/routes/role/__init__.py
View file @
0bf10239
...
...
@@ -158,3 +158,32 @@ class DeleteRole(Resource):
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/map-web-api'
,
methods
=
[
'POST'
])
class
MapWebApi
(
Resource
):
@
ns
.
doc
(
security
=
SECURITY
)
@
ns
.
expect
(
*
map_api_payload
())
@
ns
.
response
(
*
map_api_response
())
@
sanitized
()
@
fetch_user
()
def
post
(
self
,
user_id
:
int
):
try
:
validator
=
MapApiValidator
()
payload
:
dict
=
validator
.
load
(
request
.
get_json
())
map_web_api
(
payload
,
user_id
)
return
prepare_api_response
(
SUCCESS_CODE
,
'Mapped successfully'
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
\ No newline at end of file
app/routes/role/role_dao_service.py
View file @
0bf10239
from
typing
import
List
from
app
import
orm
as
ORM
from
app
import
db
from
app.app_config.constant
import
RoleTypeEnum
from
app.app_config.constant
import
EndpointTagEnum
,
RoleTypeEnum
from
app.routes.user.user_dao_service
import
get_user_by_id
...
...
@@ -116,3 +116,39 @@ def get_role_list(user_id: int) -> List[ORM.Role] | list:
return
ORM
.
Role
.
query
.
filter_by
(
is_deleted
=
False
)
.
all
()
def
map_web_api
(
payload
:
dict
,
admin_id
:
int
)
->
bool
:
admin_info
:
ORM
.
User
|
None
=
get_user_by_id
(
admin_id
)
if
not
admin_info
:
raise
Exception
(
'User not found for mapped apiKey'
)
role
:
ORM
.
Role
|
None
=
ORM
.
Role
.
query
.
filter_by
(
id
=
payload
.
get
(
'role_id'
),
is_deleted
=
False
)
.
first
()
if
not
role
:
raise
Exception
(
'Role not found'
)
if
admin_info
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
:
if
role
.
project_id
!=
admin_info
.
project_id
:
raise
Exception
(
'You have no permission to map web-api for this role'
)
web_api_list
:
List
[
ORM
.
Api
]
=
ORM
.
Api
.
query
.
filter
(
ORM
.
Api
.
id
.
in_
(
payload
.
get
(
'api_list'
)),
ORM
.
Api
.
tag
==
EndpointTagEnum
.
RA
.
value
,
)
.
all
()
if
len
(
web_api_list
)
!=
len
(
payload
.
get
(
'api_list'
)):
raise
Exception
(
'Some api not found or cannot be mapped for this role'
)
ORM
.
RoleApiMap
.
query
.
filter
(
ORM
.
RoleApiMap
.
role_id
==
role
.
id
,
)
.
delete
(
synchronize_session
=
'fetch'
)
role_api_mappings
=
[
ORM
.
RoleApiMap
(
role_id
=
role
.
id
,
api_id
=
api
.
id
,
created_by
=
admin_id
)
for
api
in
web_api_list
]
db
.
session
.
add_all
(
role_api_mappings
)
db
.
session
.
commit
()
app/routes/role/role_payload.py
View file @
0bf10239
...
...
@@ -37,3 +37,13 @@ def delete_role_payload() -> list:
parser
.
add_argument
(
'id'
,
type
=
int
,
location
=
'args'
)
return
[
parser
]
def
map_api_payload
()
->
list
:
payload
=
api
.
model
(
'mapApi Payload'
,
{
'role_id'
:
fields
.
Integer
,
'api_list'
:
fields
.
List
(
fields
.
Integer
)
})
return
[
payload
]
\ No newline at end of file
app/routes/role/role_response.py
View file @
0bf10239
...
...
@@ -62,3 +62,11 @@ def delete_role_response() -> any:
message
=
DELETE_SUCCESS_MESSAGE
,
model_name
=
'deleteRole Response'
)
def
map_api_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
'Mapped successfully'
,
model_name
=
'mapApi Response'
)
\ No newline at end of file
app/routes/role/role_validator.py
View file @
0bf10239
...
...
@@ -14,3 +14,8 @@ class UpdateRoleValidator(Schema):
class
GetRoleValidator
(
Schema
):
id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
class
MapApiValidator
(
Schema
):
role_id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
api_list
=
fields
.
List
(
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
)))
app/routes/user/__init__.py
View file @
0bf10239
...
...
@@ -253,7 +253,7 @@ class GetAllUsers(Resource):
@
ns
.
route
(
'/map-role'
,
methods
=
[
'GET'
])
class
GetProfi
le
(
Resource
):
class
MapRo
le
(
Resource
):
@
ns
.
doc
(
security
=
SECURITY
)
@
ns
.
expect
(
*
map_user_role_payload
())
...
...
app/routes/user/user_dao_service.py
View file @
0bf10239
...
...
@@ -76,14 +76,19 @@ def map_user_role(admin_id: int, user_id: int, role_id: int) -> None:
if
user_info
.
project_id
!=
admin_user
.
project_id
:
raise
Exception
(
'You are not authorized to perform this action'
)
user_role_map
=
ORM
.
UserRoleMap
.
query
.
filter
(
user_role_map
:
ORM
.
UserRoleMap
|
None
=
ORM
.
UserRoleMap
.
query
.
filter
(
ORM
.
UserRoleMap
.
user_id
==
user_id
)
.
first
()
if
user_role_map
:
user_role_map
.
role_id
=
role_id
user_role_map
.
updated_by
=
admin_user
.
id
else
:
user_role_map
=
ORM
.
UserRoleMap
(
user_id
=
user_id
,
role_id
=
role_id
)
user_role_map
=
ORM
.
UserRoleMap
(
user_id
=
user_id
,
role_id
=
role_id
,
created_by
=
admin_user
.
id
)
db
.
session
.
add
(
user_role_map
)
db
.
session
.
commit
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment