Projet

Général

Profil

0004-utils-helper-method-to-check-if-a-user-has-a-role.patch

Valentin Deniaud, 05 juin 2019 14:49

Télécharger (2,3 ko)

Voir les différences:

Subject: [PATCH 4/5] utils: helper method to check if a user has a role
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We make a distinction between roles which are obtained at the SSO,
stored in session, and roles which the user could have, statically
stored in database.

todo: ce commit dépend totalement du provisionning tel qu'implémenté par
hobo, il faut améliorer ça
 mellon/exceptions.py |  4 ++++
 mellon/utils.py      | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 mellon/exceptions.py
mellon/exceptions.py
1
class RolesNotInSession(Exception):
2

  
3
    def __init__(self, roles):
4
        self.roles = roles
mellon/utils.py
6 6
from xml.parsers import expat
7 7

  
8 8
from django.contrib import auth
9
from django.contrib.auth.models import Group
9 10
from django.core.urlresolvers import reverse
10 11
from django.template.loader import render_to_string
11 12
from django.utils.timezone import make_aware, now, make_naive, is_aware, get_default_timezone
......
14 15
import lasso
15 16

  
16 17
from . import app_settings
18
from .exceptions import RolesNotInSession
17 19

  
18 20

  
19 21
def create_metadata(request):
......
289 291
            if attribute_values & values:
290 292
                return True
291 293
    return False
294

  
295

  
296
def user_has_role(request, role_id):
297
    if request.user.is_staff and request.session.get('is_staff'):
298
        return True
299
    try:
300
        group = request.user.groups.get(id=role_id)
301
    except Group.DoesNotExist:
302
        if request.user.is_staff:
303
            raise RolesNotInSession(('staff',))
304
        return False
305
    role = getattr(group, 'role')
306
    if not role:
307
        return True
308
    if role.uuid in request.session['mellon_session']['role-slug']:
309
        return True
310
    raise RolesNotInSession((role.uuid,))
292
-