Last active
February 11, 2019 14:27
-
-
Save index0h/82c1e022d1e249a837427a5b7eb542f5 to your computer and use it in GitHub Desktop.
permissions.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Допестим для всех юзеров от партнера Partner 10 есть право Permission(20) смотреть фотки всех Gallery, это задается применру так: