Blame view

app/auth/models.py 2.11 KB
42c07429   hitier   Default role valu...
1
2
from pprint import pprint

4770ee5d   hitier   Now passwords are...
3
4
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
a5a365e8   hitier   Move User model t...
5
6
from app.models import db

7b74daf6   hitier   New User role man...
7
8
9
10
#
# Roles
#

42c07429   hitier   Default role valu...
11
12
ADMIN = 50
PROJECT = 40
7b74daf6   hitier   New User role man...
13
14
15
16
17
18
SERVICE = 30
AGENT = 10
PUBLIC = 0

_roleToName = {
    ADMIN: 'ADMIN',
42c07429   hitier   Default role valu...
19
    PROJECT: 'PROJECT',
7b74daf6   hitier   New User role man...
20
21
22
23
24
25
    SERVICE: 'SERVICE',
    AGENT: 'AGENT',
    PUBLIC: 'PUBLIC',
}
_nameToRole = {
    'ADMIN': ADMIN,
42c07429   hitier   Default role valu...
26
    'PROJECT': PROJECT,
7b74daf6   hitier   New User role man...
27
28
29
30
31
32
    'SERVICE': SERVICE,
    'AGENT': AGENT,
    'PUBLIC': PUBLIC,
}


65b2833e   hitier   Fix some sonar co...
33
def _check_role(role):
7b74daf6   hitier   New User role man...
34
35
36
37
38
39
40
41
42
43
44
    if isinstance(role, int):
        rv = role
    elif str(role) == role:
        role = role.upper()
        if role not in _nameToRole:
            raise ValueError("Unknown role: %r" % role)
        rv = _nameToRole[role]
    else:
        raise TypeError("Role not an integer or a valid string: %r" % role)
    return rv

a5a365e8   hitier   Move User model t...
45
46
47
48
49
50

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)  # primary keys are required by SQLAlchemy
    email = db.Column(db.String(100), unique=True)
    name = db.Column(db.String(100))
    login = db.Column(db.String(100), unique=True)
42c07429   hitier   Default role valu...
51
    role = db.Column(db.Integer, default=0)
615da07f   hitier   Fix set_role and ...
52
    password = db.Column(db.String(128))
4770ee5d   hitier   Now passwords are...
53
    password_hash = db.Column(db.String(128))
a5a365e8   hitier   Move User model t...
54
55
56
57

    def __repr__(self):
        return "i: {}, n: {}, e: {}, l: {}".format(self.id, self.name, self.email, self.login)

42c07429   hitier   Default role valu...
58
59
60
    # Set role at construction time
    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
615da07f   hitier   Fix set_role and ...
61
62
63
64
65
        if ('role' in kwargs):
            self.set_role(kwargs['role'])
        if ('password' in kwargs):
            self.set_password(kwargs['password'])
            self.password = None
42c07429   hitier   Default role valu...
66
67

    def set_role(self, role):
65b2833e   hitier   Fix some sonar co...
68
        self.role = _check_role(role)
7b74daf6   hitier   New User role man...
69

42c07429   hitier   Default role valu...
70
    def has_role(self, role):
65b2833e   hitier   Fix some sonar co...
71
        role = _check_role(role)
42c07429   hitier   Default role valu...
72
73
74
        return self.role == role

    def has_role_or_higher(self, role):
65b2833e   hitier   Fix some sonar co...
75
        role = _check_role(role)
42c07429   hitier   Default role valu...
76
        return self.role and (self.role >= role)
4770ee5d   hitier   Now passwords are...
77
78
79
80
81
82

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)