| 1 | [[TOC(inline)]] |
| 2 | |
| 3 | == Row Level Permissions Model == |
| 4 | |
| 5 | Row Level Permissions uses generic relations to decrease the number of tables to only one. The generic relations allow us to create row level permissions with any instance of any model and connect it to an "owner" object. To allow more freedom, the owner object is also a generic relation. This means that you can use your own models as an owner of a row level permission. The actual model code is below: |
| 6 | |
| 7 | {{{ |
| 8 | #!python |
| 9 | class RowLevelPermission(models.Model): |
| 10 | type_id = models.PositiveIntegerField("'Type' ID") |
| 11 | type_ct = models.ForeignKey(ContentType, verbose_name="'Type' content type", related_name="type_ct") |
| 12 | owner_id = models.PositiveIntegerField("'Owner' ID") |
| 13 | owner_ct = models.ForeignKey(ContentType, verbose_name="'Owner' content type", related_name="owner_ct") |
| 14 | negative = models.BooleanField() |
| 15 | permission = models.ForeignKey(Permission) |
| 16 | |
| 17 | type = models.GenericForeignKey(fk_field='type_id', ct_field='type_ct') |
| 18 | owner = models.GenericForeignKey(fk_field='owner_id', ct_field='owner_ct') |
| 19 | |
| 20 | objects = RowLevelPermissionManager() |
| 21 | |
| 22 | class Meta: |
| 23 | verbose_name = _('row level permission') |
| 24 | verbose_name_plural = _('row level permissions') |
| 25 | unique_together = (('type_ct', 'type_id', 'owner_id', 'owner_ct', 'permission'),) |
| 26 | }}} |
| 27 | |
| 28 | This does not modify the current permissions table at all, and can be layered ontop of it if the developer wishes to but it can be ignored easily. |
| 29 | |
| 30 | == How Row Permissions Are Enabled == |
| 31 | |
| 32 | Row permissions are enabled using the meta class, please see RowLevelPermissions for more information on how to enable them. How this is done is in django.db.models.base.ModelBase under __new__ is the following snippet: |
| 33 | |
| 34 | {{{ |
| 35 | #!python |
| 36 | if getattr(new_class._meta, 'row_level_permissions', None): |
| 37 | from django.contrib.auth.models import RowLevelPermission |
| 38 | gen_rel = django.db.models.GenericRelation(RowLevelPermission, object_id_field="type_id", content_type_field="type_ct") |
| 39 | new_class.add_to_class("row_level_permissions", gen_rel) |
| 40 | }}} |
| 41 | |
| 42 | In django.db.models.options.Options it has been modified to set row_level_permissions as disabled by default. |
| 43 | |
| 44 | == Owner Objects == |
| 45 | |
| 46 | At this current point of time, you can set up an owner by including the following relation: |
| 47 | {{{ |
| 48 | #!python |
| 49 | ... |
| 50 | row_level_permissions_owned = models.GenericRelation(RowLevelPermission, object_id_field="owner_id", content_type_field="owner_ct", related_name="owner") |
| 51 | ... |
| 52 | }}} |
| 53 | |
| 54 | I might be changing this around in the near future, but I only expect it to be used a few times I don't see a large need to make this a similiar process to the enabling of row level permissions on objects. Please give feedback if you think otherwise. |
| 55 | |
| 56 | == Checking of Row Level Permissions == |
| 57 | |
| 58 | To be added soon. |
| 59 | |
| 60 | == Integration into Administration Application == |
| 61 | |
| 62 | Being worked on. Will post when it is complete or near enough to have a better idea. |