Add system_wide_roles app and roles classes (#20935)

* New system_wide_roles app added in openedx/core/djangoapps
* Added SystemWideRole and SystemWideRoleAssignment classes to govern
  non-enterprise system wide roles

PROD-424
This commit is contained in:
Zainab Amir
2019-07-10 11:33:33 +05:00
committed by GitHub
parent a72198d915
commit 92c7a43011
8 changed files with 154 additions and 0 deletions

View File

@@ -1128,6 +1128,9 @@ INSTALLED_APPS = [
'lms.djangoapps.verify_student.apps.VerifyStudentConfig',
'completion',
# System Wide Roles
'openedx.core.djangoapps.system_wide_roles',
# Microsite configuration application
'microsite_configuration',

View File

@@ -2321,6 +2321,9 @@ INSTALLED_APPS = [
# defined by oauth_provider. If those tables don't exist, an error can occur.
'oauth_provider',
# System Wide Roles
'openedx.core.djangoapps.system_wide_roles',
'openedx.core.djangoapps.auth_exchange',
# For the wiki

View File

@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-07-02 09:33
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='SystemWideRole',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('name', models.CharField(db_index=True, max_length=255, unique=True)),
('description', models.TextField(blank=True, null=True)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='SystemWideRoleAssignment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('role', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='system_wide_roles.SystemWideRole')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
]

View File

@@ -0,0 +1,51 @@
"""
Django models for system wide roles.
"""
from __future__ import unicode_literals
from edx_rbac.models import UserRole, UserRoleAssignment
class SystemWideRole(UserRole): # pylint: disable=model-missing-unicode
"""
User role definitions to govern non-enterprise system wide roles.
.. no_pii:
"""
def __str__(self):
"""
Return human-readable string representation.
"""
str_representation = "<SystemWideRole {role}>"
return str_representation.format(role=self.name)
def __repr__(self):
"""
Return uniquely identifying string representation.
"""
return self.__str__()
class SystemWideRoleAssignment(UserRoleAssignment): # pylint: disable=model-missing-unicode
"""
Model to map users to a SystemWideRole.
.. no_pii:
"""
role_class = SystemWideRole
def __str__(self):
"""
Return human-readable string representation.
"""
str_representation = "<SystemWideRoleAssignment for User {user} assigned to role {role}>"
return str_representation.format(
user=self.user.id,
role=self.role.name
)
def __repr__(self):
"""
Return uniquely identifying string representation.
"""
return self.__str__()

View File

@@ -0,0 +1,50 @@
"""
Tests for system wide roles' django models.
"""
from __future__ import unicode_literals
from django.test import TestCase
from student.tests.factories import UserFactory
from openedx.core.djangoapps.system_wide_roles.models import SystemWideRole, SystemWideRoleAssignment
class SystemWideRoleTests(TestCase):
""" Tests for SystemWideRole in system_wide_roles app """
def setUp(self):
super(SystemWideRoleTests, self).setUp()
self.role = SystemWideRole.objects.create(name='TestRole')
def test_str(self):
self.assertEqual(str(self.role), '<SystemWideRole TestRole>')
def test_repr(self):
self.assertEqual(repr(self.role), '<SystemWideRole TestRole>')
class SystemWideRoleAssignmentTests(TestCase):
""" Tests for SystemWideRoleAssignment in system_wide_roles app """
def setUp(self):
super(SystemWideRoleAssignmentTests, self).setUp()
self.user = UserFactory.create()
self.role = SystemWideRole.objects.create(name='TestRole')
def test_str(self):
role_assignment = SystemWideRoleAssignment.objects.create(role=self.role, user=self.user)
self.assertEqual(
str(role_assignment),
'<SystemWideRoleAssignment for User {user} assigned to role {role}>'.format(
user=self.user.id, role=self.role.name
)
)
def test_repr(self):
role_assignment = SystemWideRoleAssignment.objects.create(role=self.role, user=self.user)
self.assertEqual(
repr(role_assignment),
'<SystemWideRoleAssignment for User {user} assigned to role {role}>'.format(
user=self.user.id, role=self.role.name
)
)