Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
re2o
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nounous
re2o
Commits
72453549
Commit
72453549
authored
Dec 28, 2017
by
LEVY-FALK Hugo
Committed by
root
Jan 06, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Décorateur d'accès à une application.
parent
3cff0683
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
+40
-3
re2o/utils.py
re2o/utils.py
+40
-3
No files found.
re2o/utils.py
View file @
72453549
...
@@ -69,7 +69,7 @@ def can_create(model):
...
@@ -69,7 +69,7 @@ def can_create(model):
return
decorator
return
decorator
def
can_edit
(
model
):
def
can_edit
(
model
,
*
field_list
):
"""Decorator to check if an user can edit a model.
"""Decorator to check if an user can edit a model.
It tries to get an instance of the model, using
It tries to get an instance of the model, using
`model.get_instance(*args, **kwargs)` and assumes that the model has a
`model.get_instance(*args, **kwargs)` and assumes that the model has a
...
@@ -91,12 +91,20 @@ def can_edit(model):
...
@@ -91,12 +91,20 @@ def can_edit(model):
return
redirect
(
reverse
(
'users:profil'
,
return
redirect
(
reverse
(
'users:profil'
,
kwargs
=
{
'userid'
:
str
(
request
.
user
.
id
)}
kwargs
=
{
'userid'
:
str
(
request
.
user
.
id
)}
))
))
for
field
in
field_list
:
can_create
=
getattr
(
model
,
'can_change_'
+
field
)
can
,
msg
=
can_create
(
instance
,
request
.
user
,
*
args
,
**
kwargs
)
if
not
can
:
messages
.
error
(
request
,
msg
or
"Vous ne pouvez pas accéder à ce menu"
)
return
redirect
(
reverse
(
'users:profil'
,
kwargs
=
{
'userid'
:
str
(
request
.
user
.
id
)}
))
return
view
(
request
,
instance
,
*
args
,
**
kwargs
)
return
view
(
request
,
instance
,
*
args
,
**
kwargs
)
return
wrapper
return
wrapper
return
decorator
return
decorator
def
can_change
(
model
,
field_list
):
def
can_change
(
model
,
*
field_list
):
"""Decorator to check if an user can edit a field of a model.
"""Decorator to check if an user can edit a field of a model.
It assumes that a valid user exists in the request and that the model has a
It assumes that a valid user exists in the request and that the model has a
method can_create(user) which returns true if the user can create this kind
method can_create(user) which returns true if the user can create this kind
...
@@ -106,7 +114,7 @@ def can_change(model, field_list):
...
@@ -106,7 +114,7 @@ def can_change(model, field_list):
def
wrapper
(
request
,
*
args
,
**
kwargs
):
def
wrapper
(
request
,
*
args
,
**
kwargs
):
for
field
in
field_list
:
for
field
in
field_list
:
can_create
=
getattr
(
model
,
'can_change_'
+
field
)
can_create
=
getattr
(
model
,
'can_change_'
+
field
)
can
,
msg
=
can_create
(
request
.
user
,
*
args
,
**
kwargs
)
can
,
msg
=
can_create
(
None
,
request
.
user
,
*
args
,
**
kwargs
)
if
not
can
:
if
not
can
:
messages
.
error
(
request
,
msg
or
"Vous ne pouvez pas accéder à ce menu"
)
messages
.
error
(
request
,
msg
or
"Vous ne pouvez pas accéder à ce menu"
)
return
redirect
(
reverse
(
'users:profil'
,
return
redirect
(
reverse
(
'users:profil'
,
...
@@ -209,6 +217,35 @@ def can_view_all(model):
...
@@ -209,6 +217,35 @@ def can_view_all(model):
return
decorator
return
decorator
APP_VIEWING_RIGHT
=
{
'cotisations'
:
'cableur'
,
'logs'
:
'cableur'
,
'machines'
:
'cableur'
,
'preferences'
:
'cableur'
,
'search'
:
'cableur'
,
'topologie'
:
'cableur'
,
'users'
:
'cableur'
,
}
def
can_view_app
(
app_name
):
"""Decorator to check if an user can view an application.
"""
assert
app_name
in
APP_VIEWING_RIGHT
.
keys
()
def
decorator
(
view
):
def
wrapper
(
request
,
*
args
,
**
kwargs
):
if
request
.
user
.
has_perms
((
APP_VIEWING_RIGHT
[
app_name
],)):
return
view
(
request
,
*
args
,
**
kwargs
)
messages
.
error
(
request
,
msg
or
"Vous ne pouvez pas accéder à l'application "
+
app_name
)
return
redirect
(
reverse
(
'users:profil'
,
kwargs
=
{
'userid'
:
str
(
request
.
user
.
id
)}
))
return
wrapper
return
decorator
def
all_adherent
(
search_time
=
DT_NOW
):
def
all_adherent
(
search_time
=
DT_NOW
):
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est
qu'une seule requete sql
qu'une seule requete sql
...
...
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