Database design of rbac component

Keywords: PHP Django

rbac is a role-based permission design, which consists of six tables. The specific table design is as follows:

from django.db import models

class Menu(models.Model):
    """
    //Menu table
    """
    title = models.CharField(verbose_name='Menu name',max_length=32,unique=True)
    icon = models.CharField(max_length=128, blank=True, null=True)

    def __str__(self):
        return self.title

class Permission(models.Model):
    """
    //Permission table
    """
    title = models.CharField(verbose_name='Permission title', max_length=32)
    url = models.CharField(verbose_name='Regular URL', max_length=128)
    name=models.CharField(verbose_name='url alias',max_length=64,unique=True) #Control permission to button
    parent=models.ForeignKey(verbose_name='Parent authority',to='self',null=True,blank=True,on_delete=models.CASCADE,limit_choices_to={'parent__isnull':True})#Build the non menu permission relationship, and expand the menu by default
    menu=models.ForeignKey(verbose_name='menu',to='Menu',null=True,blank=True,on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Role(models.Model):
    """
    //role
    """
    title = models.CharField(verbose_name='Role name', max_length=32)
    permissions = models.ManyToManyField(verbose_name='All permissions owned', to='Permission', blank=True)


    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """
    //User table
    """
    username = models.CharField(verbose_name='User name', max_length=32)
    password = models.CharField(verbose_name='Password', max_length=64)
    email = models.CharField(verbose_name='mailbox', max_length=32)
    roles = models.ManyToManyField(verbose_name='All roles owned', to=Role, blank=True)

    class Meta:
        abstract=True #in order to crm User table for inheritance

    def __str__(self):
        return self.username

Among them, user table and role table are many to many relationships, role table and permission table are many to many relationships. In addition, the permission table is associated with the menu, so that a permission url is attached to a menu, and the permission table is associated with itself, so that the permission url added, deleted and modified can be attached to a permission url.

Posted by slweb on Sat, 02 Nov 2019 14:44:12 -0700