-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.sql
67 lines (58 loc) · 2.88 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`is_group` tinyint(1) NOT NULL DEFAULT '0',
`is_protected` tinyint(1) NOT NULL DEFAULT '0',
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `fk_parent_id_role_idx` (`parent_id`),
CONSTRAINT `fk_parent_id_role` FOREIGN KEY (`parent_id`) REFERENCES `role` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `resource` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `fk_parent_id_resource_idx` (`parent_id`),
CONSTRAINT `fk_parent_id_resource` FOREIGN KEY (`parent_id`) REFERENCES `resource` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `rule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL,
`resource_id` int(11) NOT NULL,
`privilege` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_role_id_rule_idx` (`role_id`),
KEY `fk_resource_id_rule_idx` (`resource_id`),
CONSTRAINT `fk_resource_id_rule` FOREIGN KEY (`resource_id`) REFERENCES `resource` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_role_id_rule` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `user_to_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`role_id` int(11) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`deleted` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_id_user_to_role_idx` (`user_id`),
KEY `fk_role_id_user_to_role_idx` (`role_id`),
CONSTRAINT `fk_role_id_user_to_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_user_id_user_to_role` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Example
-- CREATE TABLE `user` (
-- `id` int(11) NOT NULL AUTO_INCREMENT,
-- PRIMARY KEY (`id`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `role` VALUES
(1,'super_users','Super users',1,1,NULL,NULL,NULL,NULL),
(2,'administrators','Administrators',1,1,1,NULL,NULL,NULL),
(3,'members','Members',1,1,2,NULL,NULL,NULL),
(4,'guests','Guests',1,1,3,NULL,NULL,NULL);
INSERT INTO `resource` VALUES (1,0,'api/v1/countries','',NULL,NULL,NULL);
INSERT INTO `rule` VALUES (1,3,1,'read',NULL,NULL,NULL)