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
7ccdc95c
Commit
7ccdc95c
authored
Jul 22, 2024
by
deepdey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add user-role-map api
parent
4c494289
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
135 additions
and
55 deletions
+135
-55
app/routes/user/__init__.py
app/routes/user/__init__.py
+58
-40
app/routes/user/user_dao_service.py
app/routes/user/user_dao_service.py
+47
-10
app/routes/user/user_payload.py
app/routes/user/user_payload.py
+8
-0
app/routes/user/user_response.py
app/routes/user/user_response.py
+17
-4
app/routes/user/user_validator.py
app/routes/user/user_validator.py
+5
-1
No files found.
app/routes/user/__init__.py
View file @
7ccdc95c
...
...
@@ -29,7 +29,7 @@ logger = get_logger(__name__)
ns
=
api
.
namespace
(
name
=
os
.
path
.
dirname
(
__file__
)
.
split
(
os
.
sep
)[
-
1
]
.
replace
(
'_'
,
'-'
),
description
=
'API connected to user module'
,
ordered
=
False
ordered
=
False
,
)
...
...
@@ -54,26 +54,24 @@ class Login(Resource):
user_info
:
ORM
.
User
|
None
=
get_user_by_email
(
email
)
if
(
not
user_info
)
:
if
not
user_info
:
return
prepare_api_response
(
401
,
'Invalid Email'
)
if
not
bcrypt
.
check_password_hash
(
user_info
.
password
,
password
):
return
prepare_api_response
(
401
,
'Invalid password'
)
api_key
=
self
.
generate_api_key
()
process_valid_loging
(
user_info
,
api_key
)
return
prepare_api_response
(
SUCCESS_CODE
,
SUCCESS_MESSAGE
,
data
=
{
'apiKey'
:
api_key
}
SUCCESS_CODE
,
SUCCESS_MESSAGE
,
data
=
{
'apiKey'
:
api_key
}
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
error
=
e
.
normalized_messages
()
,
)
except
Exception
as
e
:
...
...
@@ -99,33 +97,29 @@ class LoginOTP(Resource):
user_info
:
ORM
.
User
|
None
=
get_user_by_email
(
email
)
if
(
not
user_info
)
:
if
not
user_info
:
return
prepare_api_response
(
401
,
'Invalid Email'
)
otp
:
int
=
self
.
generate_otp
()
send_otp
(
'shadowtitan3003@gmail.com'
,
[
email
],
otp
)
redis_key
:
str
=
str
(
user_info
.
id
)
+
'_'
+
'otp'
+
str
(
otp
)
redis_key
:
str
=
str
(
user_info
.
id
)
+
'_'
+
'otp'
+
str
(
otp
)
set_value
(
redis_key
,
1
,
60
*
5
)
return
prepare_api_response
(
SUCCESS_CODE
,
'OTP generated successfully'
)
return
prepare_api_response
(
SUCCESS_CODE
,
'OTP generated successfully'
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
error
=
e
.
normalized_messages
()
,
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/login-otp-verify'
,
methods
=
[
'POST'
])
class
LoginOTPVerify
(
Resource
):
...
...
@@ -146,37 +140,34 @@ class LoginOTPVerify(Resource):
user_info
:
ORM
.
User
|
None
=
get_user_by_email
(
email
)
if
(
not
user_info
)
:
if
not
user_info
:
return
prepare_api_response
(
401
,
'Invalid Email'
)
redis_key
:
str
=
str
(
user_info
.
id
)
+
'_'
+
'otp'
+
str
(
provided_otp
)
redis_otp
:
int
|
None
=
get_value
(
redis_key
)
if
redis_otp
is
None
:
raise
Exception
(
'OTP is invalid.'
)
raise
Exception
(
'OTP is invalid.'
)
api_key
=
self
.
generate_api_key
()
process_valid_loging
(
user_info
,
api_key
)
return
prepare_api_response
(
SUCCESS_CODE
,
'OTP Verified Successfully'
,
data
=
{
'apiKey'
:
api_key
}
SUCCESS_CODE
,
'OTP Verified Successfully'
,
data
=
{
'apiKey'
:
api_key
}
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
error
=
e
.
normalized_messages
()
,
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/profile'
,
methods
=
[
'GET'
])
class
GetProfile
(
Resource
):
...
...
@@ -200,7 +191,7 @@ class GetProfile(Resource):
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/get'
,
methods
=
[
'GET'
])
class
GetUser
(
Resource
):
...
...
@@ -215,8 +206,9 @@ class GetUser(Resource):
validator
=
GetUserValidator
()
arguments
:
dict
=
validator
.
load
(
request
.
args
)
user_info
:
ORM
.
User
|
None
=
\
get_user_by_id_for_admin
(
user_id
,
arguments
[
'id'
])
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'
)
...
...
@@ -235,7 +227,7 @@ class GetUser(Resource):
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
):
...
...
@@ -252,12 +244,38 @@ class GetAllUsers(Resource):
return
prepare_api_response
(
SUCCESS_CODE
,
GET_ALL_SUCCESS_MESSAGE
,
data
=
[
user
.
to_dict
(
rules
=
(
'-user_roles'
,))
for
user
in
users_list
]
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
@
ns
.
route
(
'/map-role'
,
methods
=
[
'GET'
])
class
GetProfile
(
Resource
):
@
ns
.
doc
(
security
=
SECURITY
)
@
ns
.
expect
(
*
map_user_role_payload
())
@
ns
.
response
(
*
map_user_role_response
())
@
fetch_user
()
@
sanitized
()
def
get
(
self
,
user_id
:
int
):
try
:
validator
=
MapUserRoleValidator
()
payload
:
dict
=
validator
.
load
(
request
.
args
)
map_user_role
(
user_id
,
payload
[
'user_id'
],
payload
[
'role_id'
])
return
prepare_api_response
(
SUCCESS_CODE
,
'Role 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
))
app/routes/user/user_dao_service.py
View file @
7ccdc95c
from
typing
import
List
from
app
import
orm
as
ORM
from
app
import
db
from
app.app_config.constant
import
RoleTypeEnum
...
...
@@ -23,10 +24,10 @@ def get_user_by_id_for_admin(admin_id: int, user_id: int) -> ORM.User | None:
if
admin_user
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
:
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
,
ORM
.
User
.
id
==
user_id
,
ORM
.
User
.
project_id
==
admin_user
.
project_id
)
.
first
()
ORM
.
User
.
is_deleted
==
False
,
ORM
.
User
.
id
==
user_id
,
ORM
.
User
.
project_id
==
admin_user
.
project_id
,
)
.
first
()
else
:
# return all user data for super admin
return
ORM
.
User
.
query
.
filter
(
...
...
@@ -42,11 +43,47 @@ def get_user_list_for_project_admin(admin_id: int) -> List[ORM.User]:
if
admin_user
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
:
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
,
ORM
.
User
.
project_id
==
admin_user
.
project_id
)
.
all
()
ORM
.
User
.
is_deleted
==
False
,
ORM
.
User
.
project_id
==
admin_user
.
project_id
)
.
all
()
else
:
# return all user data for super admin
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
)
.
all
()
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
)
.
all
()
def
map_user_role
(
admin_id
:
int
,
user_id
:
int
,
role_id
:
int
)
->
None
:
admin_user
:
ORM
.
User
|
None
=
get_user_by_id
(
admin_id
)
if
not
admin_user
:
raise
Exception
(
'User not found for mapped apiKey'
)
user_info
:
ORM
.
User
|
None
=
get_user_by_id
(
user_id
)
if
not
user_info
:
raise
Exception
(
'User not found for given user_id'
)
role_info
:
ORM
.
Role
|
None
=
ORM
.
Role
.
query
.
filter_by
(
id
=
role_id
)
.
first
()
if
not
role_info
:
raise
Exception
(
'Role not found for given role_id'
)
if
user_info
.
role_type_id
!=
role_info
.
role_type_id
:
raise
Exception
(
'The role-type for user and role does not match'
)
if
user_info
.
project_id
!=
role_info
.
project_id
:
raise
Exception
(
'This role cannot be mapped for this user'
)
if
admin_user
.
role_type
.
role_name
!=
RoleTypeEnum
.
SUPER_ADMIN
.
value
:
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
(
ORM
.
UserRoleMap
.
user_id
==
user_id
)
.
first
()
if
user_role_map
:
user_role_map
.
role_id
=
role_id
else
:
user_role_map
=
ORM
.
UserRoleMap
(
user_id
=
user_id
,
role_id
=
role_id
)
db
.
session
.
add
(
user_role_map
)
db
.
session
.
commit
()
app/routes/user/user_payload.py
View file @
7ccdc95c
...
...
@@ -33,4 +33,12 @@ def get_user_payload() -> list:
parser
=
api
.
parser
()
parser
.
add_argument
(
'id'
,
required
=
True
,
type
=
int
,
location
=
'args'
)
return
[
parser
]
def
map_user_role_payload
()
->
list
:
parser
=
api
.
parser
()
parser
.
add_argument
(
'user_id'
,
required
=
True
,
type
=
int
,
location
=
'args'
)
parser
.
add_argument
(
'role_id'
,
required
=
True
,
type
=
int
,
location
=
'args'
)
return
[
parser
]
\ No newline at end of file
app/routes/user/user_response.py
View file @
7ccdc95c
...
...
@@ -40,22 +40,25 @@ def login_response() -> any:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
SUCCESS_MESSAGE
,
model_name
=
'login
OTP Response'
,
model_name
=
'loginOTP Response'
,
data
=
fields
.
Nested
(
LOGIN_RESPONSE_MODEL
),
)
def
otp_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
'OTP generated successfully'
,
model_name
=
'Verify
OTP Response'
,
model_name
=
'VerifyOTP Response'
,
data
=
fields
.
Nested
(
LOGIN_RESPONSE_MODEL
),
)
def
otp_verify_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
'OTP Verified Successfully'
,
model_name
=
'login
OTP
Verify Response'
,
model_name
=
'login
OTP
Verify Response'
,
data
=
fields
.
Nested
(
LOGIN_RESPONSE_MODEL
),
)
...
...
@@ -68,13 +71,23 @@ def get_user_response() -> any:
data
=
fields
.
Nested
(
USER_RESPONSE_MODEL
),
)
def
get_user_profile_response
()
->
any
:
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
)),
)
def
map_user_role_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
'Role mapped Successfully'
,
model_name
=
'roleMap Response'
)
\ No newline at end of file
app/routes/user/user_validator.py
View file @
7ccdc95c
...
...
@@ -13,4 +13,8 @@ class OtpVerifyValidator(Schema):
OTP
=
fields
.
Integer
(
required
=
True
)
class
GetUserValidator
(
Schema
):
id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
\ No newline at end of file
id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
class
MapUserRoleValidator
(
Schema
):
user_id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
role_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