Skip to content

Instantly share code, notes, and snippets.

@index0h
Last active February 11, 2019 14:27
Show Gist options
  • Save index0h/82c1e022d1e249a837427a5b7eb542f5 to your computer and use it in GitHub Desktop.
Save index0h/82c1e022d1e249a837427a5b7eb542f5 to your computer and use it in GitHub Desktop.
permissions.sql
CREATE TABLE IF NOT EXISTS `gallery` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`galleryId` INT UNSIGNED NOT NULL,
`createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`name` VARCHAR(64) NULL,
PRIMARY KEY (`id`),
KEY `galleryId` (`galleryId`)
)
ENGINE = InnoDB
DEFAULT CHARSET = `utf8`;
CREATE TABLE IF NOT EXISTS `photo` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`publicId` BINARY(16) NOT NULL,
`galleryId` INT UNSIGNED NOT NULL,
`createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `galleryId` (`galleryId`)
)
ENGINE = InnoDB
DEFAULT CHARSET = `utf8`;
CREATE TABLE IF NOT EXISTS `permission` (
`permissionOwnerId` INT UNSIGNED NOT NULL,
`permissionOwnerType` TINYINT UNSIGNED NOT NULL,
`permission` TINYINT UNSIGNED NOT NULL,
`isEnabled` TINYINT UNSIGNED NOT NULL,
`resourceOwnerId` INT UNSIGNED NOT NULL,
`resourceOwnerType` TINYINT UNSIGNED NOT NULL,
`resourceType` TINYINT UNSIGNED NOT NULL,
`resourceId` INT UNSIGNED NOT NULL,
PRIMARY KEY (`permissionOwnerId`, `permissionOwnerType`, `permission`, `isEnabled`, `resourceOwnerId`, `resourceOwnerType`, `resourceType`, `resourceId`)
)
ENGINE = InnoDB
DEFAULT CHARSET = `utf8`;
SELECT `resourceOwnerId`
FROM `permission`
WHERE `permissionOwnerId` = 'PERMISSION_OWNER_ID'
AND `permission` IN('PERMISSION', 'ANY')
AND `isEnabled` = 1
GROUP BY `resourceOwnerId`;
SELECT
p.*
FROM `photo` AS `p`
INNER JOIN `gallery` AS `g` ON `g`.`id` = `p`.`galleryId`
INNER JOIN `permission`
ON `permission`.`permissionOwnerId` IN ('PERMISSION_OWNER_ID', 'ANY') -- PERMISSION_OWNER_ID - это id пользователя которым запрашиваем, а так же его групп
AND `permission`.`permission` IN('PERMISSION', 'ANY')
AND `permission`.`isEnabled` = 1
AND `permission`.`resourceOwnerId` IN ('RESOURCE_OWNER_ID', 'ANY')
AND `permission`.`resourceType` IN('GALLERY', 'ANY')
AND `permission`.`resourceId` IN (`g`.`id`, 'ANY');
@index0h
Copy link
Author

index0h commented Feb 11, 2019

permissionOwnerId permissionOwnerType permission isEnabled resourceOwnerId resourceOwnerType resourceType resourceId Описание
10 0 0 1 0 0 0 0 У пользователя 10 - права на все, грубо говоря он - root
0 1 0 1 0 0 0 0 У пользователей типа 1 права на все
10 1 2 1 0 0 0 0 У пользователя 10 типа 1 есть право 2 на все ресурсы

@index0h
Copy link
Author

index0h commented Feb 11, 2019

  • permissionOwnerId - юзер / группа / партнер ... кому пренадлежит право
  • permissionOwnerType - тип юзера, которому принадлежит право
  • permission - идентификатор права, если 0 - это все права
  • isEnabled - флаг, включено ли право
  • resourceOwnerId - юзер / группа / партнер ... кому пренадлежит ресурс
  • resourceOwnerType - тип юзера, которому пренадлежит ресурс
  • resourceType - тип ресурса, на который распространяется право
  • resourceId - идентификатор ресурса, на который распространяется право

Допестим для всех юзеров от партнера Partner 10 есть право Permission(20) смотреть фотки всех Gallery, это задается применру так:

permissionOwnerId   - 10
permissionOwnerType - UserType::Partner
permission          - 20
isEnabled           - 1
resourceOwnerId     - 0
resourceOwnerType   - 0
resourceType        - ResourceType::Gallery
resourceId          - 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment