Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

""" 

    marvin.permissions 

    ~~~~~~~~~~~~~~~~~~ 

 

    Permissions used around marvin. 

 

""" 

 

from flask import g 

from flask.ext.principal import identity_changed, UserNeed 

 

from functools import wraps 

 

 

@identity_changed.connect 

def on_identity_loaded(sender, identity): # pylint: disable=unused-argument 

    """ When an identify is loaded, initialize it with it's permissions. """ 

 

    identity.user = g.user 

 

    # Add the userneed 

    identity.provides.add(UserNeed(g.user.id)) 

 

 

def login_required(func): 

    """ If you decorate a view with this, it will ensure that the current user is 

    logged in and authenticated before calling the actual view. 

 

    :param func: The view function to decorate. 

    :type func: function 

    """ 

 

    @wraps(func) 

    def decorated_view(*args, **kwargs): 

        """ View that only grants access to authenticated users. """ 

        if g.user.is_authenticated(): 

            return func(*args, **kwargs) 

        else: 

            return { 

                'msg': 'You must pass an auth_token to be able to access this endpoint', 

            }, 401 

    return decorated_view