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 | from app.models import db | 4 | from app.models import db |
3 | 5 | ||
4 | # | 6 | # |
5 | # Roles | 7 | # Roles |
6 | # | 8 | # |
7 | 9 | ||
8 | -ADMIN = 40 | 10 | +ADMIN = 50 |
11 | +PROJECT = 40 | ||
9 | SERVICE = 30 | 12 | SERVICE = 30 |
10 | AGENT = 10 | 13 | AGENT = 10 |
11 | PUBLIC = 0 | 14 | PUBLIC = 0 |
12 | 15 | ||
13 | _roleToName = { | 16 | _roleToName = { |
14 | ADMIN: 'ADMIN', | 17 | ADMIN: 'ADMIN', |
18 | + PROJECT: 'PROJECT', | ||
15 | SERVICE: 'SERVICE', | 19 | SERVICE: 'SERVICE', |
16 | AGENT: 'AGENT', | 20 | AGENT: 'AGENT', |
17 | PUBLIC: 'PUBLIC', | 21 | PUBLIC: 'PUBLIC', |
18 | } | 22 | } |
19 | _nameToRole = { | 23 | _nameToRole = { |
20 | 'ADMIN': ADMIN, | 24 | 'ADMIN': ADMIN, |
25 | + 'PROJECT': PROJECT, | ||
21 | 'SERVICE': SERVICE, | 26 | 'SERVICE': SERVICE, |
22 | 'AGENT': AGENT, | 27 | 'AGENT': AGENT, |
23 | 'PUBLIC': PUBLIC, | 28 | 'PUBLIC': PUBLIC, |
@@ -43,14 +48,23 @@ class User(UserMixin, db.Model): | @@ -43,14 +48,23 @@ class User(UserMixin, db.Model): | ||
43 | name = db.Column(db.String(100)) | 48 | name = db.Column(db.String(100)) |
44 | login = db.Column(db.String(100), unique=True) | 49 | login = db.Column(db.String(100), unique=True) |
45 | password = db.Column(db.String(100)) | 50 | password = db.Column(db.String(100)) |
46 | - role = db.Column(db.Integer) | 51 | + role = db.Column(db.Integer, default=0) |
47 | 52 | ||
48 | def __repr__(self): | 53 | def __repr__(self): |
49 | return "i: {}, n: {}, e: {}, l: {}".format(self.id, self.name, self.email, self.login) | 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 | self.role = _checkRole(role) | 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 | role = _checkRole(role) | 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,7 +123,7 @@ def user_delete(user_id): | ||
123 | def create_db(): | 123 | def create_db(): |
124 | """ Create the database structure.""" | 124 | """ Create the database structure.""" |
125 | db.create_all() | 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 | db.session.add(admin) | 127 | db.session.add(admin) |
128 | db.session.commit() | 128 | db.session.commit() |
129 | 129 |
tests/backend_tests.py
@@ -11,7 +11,7 @@ class BaseTestCase(unittest.TestCase): | @@ -11,7 +11,7 @@ class BaseTestCase(unittest.TestCase): | ||
11 | self.app_context = self.app.app_context() | 11 | self.app_context = self.app.app_context() |
12 | self.app_context.push() | 12 | self.app_context.push() |
13 | db.create_all() | 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 | db.session.add(admin) | 15 | db.session.add(admin) |
16 | db.session.commit() | 16 | db.session.commit() |
17 | 17 | ||
@@ -38,14 +38,14 @@ class AuthModelTestCase(BaseTestCase): | @@ -38,14 +38,14 @@ class AuthModelTestCase(BaseTestCase): | ||
38 | 38 | ||
39 | def test_setrole(self): | 39 | def test_setrole(self): |
40 | admin = User.query.filter(User.name == 'admin').one_or_none() | 40 | admin = User.query.filter(User.name == 'admin').one_or_none() |
41 | - admin.setRole("ADMIN") | 41 | + admin.set_role("ADMIN") |
42 | db.session.commit() | 42 | db.session.commit() |
43 | admin = User.query.filter(User.name == 'admin').one_or_none() | 43 | admin = User.query.filter(User.name == 'admin').one_or_none() |
44 | self.assertTrue(admin is not None) | 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 | def test_setrole_valueerror(self): | 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 | with self.assertRaises(ValueError) as ve: | 50 | with self.assertRaises(ValueError) as ve: |
51 | - admin.setRole("NOSUCHROLE") | 51 | + admin.set_role("NOSUCHROLE") |