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
176c2cc3
Commit
176c2cc3
authored
Jul 14, 2024
by
deepdey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(user): add get-all and get-by-id api
parent
ccb18766
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
175 additions
and
3 deletions
+175
-3
app/app_config/constant.py
app/app_config/constant.py
+9
-1
app/routes/user/__init__.py
app/routes/user/__init__.py
+62
-1
app/routes/user/user_dao_service.py
app/routes/user/user_dao_service.py
+84
-0
app/routes/user/user_payload.py
app/routes/user/user_payload.py
+7
-0
app/routes/user/user_response.py
app/routes/user/user_response.py
+9
-1
app/routes/user/user_validator.py
app/routes/user/user_validator.py
+4
-0
No files found.
app/app_config/constant.py
View file @
176c2cc3
from
enum
import
Enum
ALPHANUMARIC_STRING
:
str
=
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
ENDPOINT_TAGS
:
list
=
[
'P'
,
'V'
,
'SU'
,
'PV'
,
'RA'
]
ENDPOINT_METHODS
:
list
=
[
'GET'
,
'POST'
,
'PATCH'
,
'PUT'
,
'DELETE'
]
\ No newline at end of file
ENDPOINT_METHODS
:
list
=
[
'GET'
,
'POST'
,
'PATCH'
,
'PUT'
,
'DELETE'
]
class
RoleTypeEnum
(
Enum
):
SUPER_ADMIN
=
'SUPER_ADMIN'
PROJECT_ADMIN
=
'PROJECT_ADMIN'
PROJECT_STAFF
=
'PROJECT_STAFF'
PROJECT_USER
=
'PROJECT_USER'
\ No newline at end of file
app/routes/user/__init__.py
View file @
176c2cc3
...
...
@@ -99,4 +99,65 @@ class GetProfile(Resource):
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
\ No newline at end of file
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/get'
,
methods
=
[
'GET'
])
class
GetUser
(
Resource
):
@
ns
.
doc
(
security
=
SECURITY
)
@
ns
.
expect
(
*
get_user_payload
())
@
ns
.
response
(
*
get_user_response
())
@
sanitized
()
@
fetch_user
()
def
get
(
self
,
user_id
:
int
):
try
:
validator
=
GetUserValidator
()
arguments
:
dict
=
validator
.
load
(
request
.
args
)
user_info
:
ORM
.
User
|
None
=
\
get_user_by_id_for_admin
(
user_id
,
arguments
[
'id'
])
if
not
user_info
:
return
prepare_api_response
(
404
,
'User not found'
)
return
prepare_api_response
(
SUCCESS_CODE
,
GET_SUCCESS_MESSAGE
,
data
=
user_info
.
to_dict
()
)
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
))
@
ns
.
route
(
'/get-all'
,
methods
=
[
'GET'
])
class
GetAllUsers
(
Resource
):
@
ns
.
doc
(
security
=
SECURITY
)
@
ns
.
response
(
*
get_user_list_response
())
@
sanitized
()
@
fetch_user
()
def
get
(
self
,
user_id
:
int
):
try
:
users_list
:
List
[
ORM
.
User
]
=
get_user_list_for_project_admin
(
user_id
)
return
prepare_api_response
(
SUCCESS_CODE
,
GET_ALL_SUCCESS_MESSAGE
,
data
=
[
user
.
to_dict
(
rules
=
(
'-user_roles'
,))
for
user
in
users_list
]
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
# TODO: implement forget password and registration api
\ No newline at end of file
app/routes/user/user_dao_service.py
View file @
176c2cc3
from
typing
import
List
from
app
import
orm
as
ORM
from
app.app_config.constant
import
RoleTypeEnum
def
get_user_by_username
(
username
:
str
)
->
ORM
.
User
|
None
:
...
...
@@ -8,5 +10,87 @@ def get_user_by_username(username: str) -> ORM.User | None:
def
get_user_by_email
(
email
:
str
)
->
ORM
.
User
|
None
:
return
ORM
.
User
.
query
.
filter_by
(
email
=
email
,
is_deleted
=
False
)
.
first
()
def
get_user_by_id
(
id
:
int
)
->
ORM
.
User
|
None
:
return
ORM
.
User
.
query
.
filter_by
(
id
=
id
,
is_deleted
=
False
)
.
first
()
def
get_user_by_id_for_admin
(
admin_id
:
int
,
user_id
:
int
)
->
ORM
.
User
|
None
:
admin_user
:
ORM
.
User
|
None
=
get_user_by_id
(
admin_id
)
if
not
admin_user
:
raise
Exception
(
'User not found for mapped apiKey'
)
if
all
(
user_role
.
role
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
for
user_role
in
admin_user
.
user_roles
):
if
all
(
user_role
.
role
.
role_type
.
role_name
!=
RoleTypeEnum
.
PROJECT_ADMIN
.
value
for
user_role
in
admin_user
.
user_roles
):
raise
Exception
(
'You do not have permission to view user details'
)
else
:
# return all user data for project admin for his specific project
subquery
=
(
ORM
.
db
.
session
.
query
(
ORM
.
Role
.
project_id
)
.
join
(
ORM
.
UserRoleMap
)
.
filter
(
ORM
.
UserRoleMap
.
user_id
==
admin_id
)
.
subquery
()
)
return
(
ORM
.
db
.
session
.
query
(
ORM
.
User
)
.
join
(
ORM
.
UserRoleMap
)
.
join
(
ORM
.
Role
)
.
filter
(
ORM
.
Role
.
project_id
==
subquery
,
ORM
.
User
.
id
==
user_id
,
ORM
.
User
.
is_deleted
==
False
,
)
.
first
()
)
else
:
# return all user data for super admin
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
,
ORM
.
User
.
id
==
user_id
)
.
first
()
def
get_user_list_for_project_admin
(
admin_id
:
int
)
->
List
[
ORM
.
User
]:
admin_user
:
ORM
.
User
|
None
=
get_user_by_id
(
admin_id
)
if
not
admin_user
:
raise
Exception
(
'User not found for mapped apiKey'
)
if
all
(
user_role
.
role
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
for
user_role
in
admin_user
.
user_roles
):
if
all
(
user_role
.
role
.
role_type
.
role_name
!=
RoleTypeEnum
.
PROJECT_ADMIN
.
value
for
user_role
in
admin_user
.
user_roles
):
raise
Exception
(
'You do not have permission to view user list'
)
else
:
# return all user data for project admin for his specific project
subquery
=
(
ORM
.
db
.
session
.
query
(
ORM
.
Role
.
project_id
)
.
join
(
ORM
.
UserRoleMap
)
.
filter
(
ORM
.
UserRoleMap
.
user_id
==
admin_id
)
.
subquery
()
)
return
(
ORM
.
db
.
session
.
query
(
ORM
.
User
)
.
join
(
ORM
.
UserRoleMap
)
.
join
(
ORM
.
Role
)
.
filter
(
ORM
.
Role
.
project_id
==
subquery
,
ORM
.
User
.
is_deleted
==
False
,
)
.
all
()
)
else
:
# return all user data for super admin
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
)
.
all
()
app/routes/user/user_payload.py
View file @
176c2cc3
...
...
@@ -9,3 +9,10 @@ def login_payload() -> list:
)
return
[
payload
]
def
get_user_payload
()
->
list
:
parser
=
api
.
parser
()
parser
.
add_argument
(
'id'
,
required
=
True
,
type
=
int
,
location
=
'args'
)
return
[
parser
]
\ No newline at end of file
app/routes/user/user_response.py
View file @
176c2cc3
...
...
@@ -47,4 +47,12 @@ def get_user_response() -> any:
)
def
get_user_profile_response
()
->
any
:
return
get_user_response
()
\ No newline at end of file
return
get_user_response
()
def
get_user_list_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
GET_ALL_SUCCESS_MESSAGE
,
model_name
=
'getUserList Response'
,
data
=
fields
.
List
(
fields
.
Nested
(
USER_RESPONSE_MODEL
)),
)
\ No newline at end of file
app/routes/user/user_validator.py
View file @
176c2cc3
...
...
@@ -4,3 +4,7 @@ from marshmallow import Schema, fields, validate
class
LoginValidator
(
Schema
):
email
=
fields
.
String
(
required
=
True
)
password
=
fields
.
String
(
required
=
True
)
class
GetUserValidator
(
Schema
):
id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
\ No newline at end of file
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