Commit 42c074295ca4d1df1f3b50e4bbc3c974eb3b7c9c
1 parent
3423bf9d
Exists in
master
and in
4 other branches
Default role value, and new role methods
Showing
3 changed files
with
27 additions
and
13 deletions
Show diff stats
app/auth/models.py
1 | -from flask_login import UserMixin | |
1 | +from pprint import pprint | |
2 | + | |
3 | +from flask_login import UserMixin, current_user | |
2 | 4 | from app.models import db |
3 | 5 | |
4 | 6 | # |
5 | 7 | # Roles |
6 | 8 | # |
7 | 9 | |
8 | -ADMIN = 40 | |
10 | +ADMIN = 50 | |
11 | +PROJECT = 40 | |
9 | 12 | SERVICE = 30 |
10 | 13 | AGENT = 10 |
11 | 14 | PUBLIC = 0 |
12 | 15 | |
13 | 16 | _roleToName = { |
14 | 17 | ADMIN: 'ADMIN', |
18 | + PROJECT: 'PROJECT', | |
15 | 19 | SERVICE: 'SERVICE', |
16 | 20 | AGENT: 'AGENT', |
17 | 21 | PUBLIC: 'PUBLIC', |
18 | 22 | } |
19 | 23 | _nameToRole = { |
20 | 24 | 'ADMIN': ADMIN, |
25 | + 'PROJECT': PROJECT, | |
21 | 26 | 'SERVICE': SERVICE, |
22 | 27 | 'AGENT': AGENT, |
23 | 28 | 'PUBLIC': PUBLIC, |
... | ... | @@ -43,14 +48,23 @@ class User(UserMixin, db.Model): |
43 | 48 | name = db.Column(db.String(100)) |
44 | 49 | login = db.Column(db.String(100), unique=True) |
45 | 50 | password = db.Column(db.String(100)) |
46 | - role = db.Column(db.Integer) | |
51 | + role = db.Column(db.Integer, default=0) | |
47 | 52 | |
48 | 53 | def __repr__(self): |
49 | 54 | return "i: {}, n: {}, e: {}, l: {}".format(self.id, self.name, self.email, self.login) |
50 | 55 | |
51 | - def setRole(self, role): | |
56 | + # Set role at construction time | |
57 | + def __init__(self, **kwargs): | |
58 | + super(User, self).__init__(**kwargs) | |
59 | + self.set_role(kwargs['role']) | |
60 | + | |
61 | + def set_role(self, role): | |
52 | 62 | self.role = _checkRole(role) |
53 | 63 | |
54 | - def hasRole(self, role): | |
64 | + def has_role(self, role): | |
65 | + role = _checkRole(role) | |
66 | + return self.role == role | |
67 | + | |
68 | + def has_role_or_higher(self, role): | |
55 | 69 | role = _checkRole(role) |
56 | - return role == self.role | |
70 | + return self.role and (self.role >= role) | ... | ... |
app/commands/commands.py
... | ... | @@ -123,7 +123,7 @@ def user_delete(user_id): |
123 | 123 | def create_db(): |
124 | 124 | """ Create the database structure.""" |
125 | 125 | db.create_all() |
126 | - admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') | |
126 | + admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') | |
127 | 127 | db.session.add(admin) |
128 | 128 | db.session.commit() |
129 | 129 | ... | ... |
tests/backend_tests.py
... | ... | @@ -11,7 +11,7 @@ class BaseTestCase(unittest.TestCase): |
11 | 11 | self.app_context = self.app.app_context() |
12 | 12 | self.app_context.push() |
13 | 13 | db.create_all() |
14 | - admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') | |
14 | + admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') | |
15 | 15 | db.session.add(admin) |
16 | 16 | db.session.commit() |
17 | 17 | |
... | ... | @@ -38,14 +38,14 @@ class AuthModelTestCase(BaseTestCase): |
38 | 38 | |
39 | 39 | def test_setrole(self): |
40 | 40 | admin = User.query.filter(User.name == 'admin').one_or_none() |
41 | - admin.setRole("ADMIN") | |
41 | + admin.set_role("ADMIN") | |
42 | 42 | db.session.commit() |
43 | 43 | admin = User.query.filter(User.name == 'admin').one_or_none() |
44 | 44 | self.assertTrue(admin is not None) |
45 | - self.assertTrue(admin.hasRole("ADMIN")) | |
46 | - self.assertFalse(admin.hasRole("SERVICE")) | |
45 | + self.assertTrue(admin.has_role("ADMIN")) | |
46 | + self.assertFalse(admin.has_role("SERVICE")) | |
47 | 47 | |
48 | 48 | def test_setrole_valueerror(self): |
49 | - admin = User(email='me@nowhere.org', name='me', login='me', password='me') | |
49 | + admin = User(email='me@nowhere.org', name='me', login='me', password='me', role='admin') | |
50 | 50 | with self.assertRaises(ValueError) as ve: |
51 | - admin.setRole("NOSUCHROLE") | |
51 | + admin.set_role("NOSUCHROLE") | ... | ... |