Made changes to the database and added models to support options. Next is permissions

git-svn-id: file:///svn-source/pmgr/branches/pre_0.1_work_20090819@797 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-28 01:42:29 +00:00
parent f5f09421c1
commit 1d4dcbd2b0
14 changed files with 1027 additions and 119 deletions

View File

@@ -157,3 +157,422 @@ WHERE
-- catch other types not considered in this query
OR T.type NOT IN ('RECEIPT', 'CREDIT_NOTE', 'INVOICE', 'PAYMENT')
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- #################################################################
-- ## USER / GROUP
INSERT INTO pmgr_groups (`code`, `name`, `rank`)
VALUES('Owner', 'Owner Group', 25);
SET @o_gid = LAST_INSERT_ID();
INSERT INTO pmgr_groups (`code`, `name`, `rank`)
VALUES('Admin', 'Admin Group', 50);
SET @a_gid = LAST_INSERT_ID();
INSERT INTO pmgr_groups (`code`, `name`, `rank`)
VALUES('Manager', 'Manager Group', 75);
SET @m_gid = LAST_INSERT_ID();
INSERT INTO pmgr_groups (`code`, `name`)
VALUES('Temp', 'Temporary Help');
SET @t_gid = LAST_INSERT_ID();
INSERT INTO pmgr_users (`code`, `login`, `contact_id`)
VALUES('AP', 'abijah', 0);
SET @a_uid = LAST_INSERT_ID();
INSERT INTO pmgr_users (`code`, `login`, `contact_id`)
VALUES('SK', 'shirley', 0);
SET @s_uid = LAST_INSERT_ID();
INSERT INTO pmgr_users (`code`, `login`, `contact_id`)
VALUES('DE', 'dan', 0);
SET @d_uid = LAST_INSERT_ID();
INSERT INTO pmgr_users (`code`, `login`, `contact_id`)
VALUES('KD', 'kevin', 0);
SET @k_uid = LAST_INSERT_ID();
INSERT INTO pmgr_sites (`code`, `name`)
VALUES('VSS', 'Valley Storage');
SET @v_sid = LAST_INSERT_ID();
INSERT INTO pmgr_sites (`code`, `name`)
VALUES('FAKE', 'Fake Site');
SET @f_sid = LAST_INSERT_ID();
-- Site Membership
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@v_sid, @a_uid, @o_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@v_sid, @a_uid, @a_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@v_sid, @a_uid, @m_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@v_sid, @s_uid, @m_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@v_sid, @d_uid, @t_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@f_sid, @s_uid, @a_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@f_sid, @s_uid, @m_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@f_sid, @k_uid, @o_gid);
INSERT INTO pmgr_site_memberships (`site_id`, `user_id`, `group_id`)
VALUES(@f_sid, @d_uid, @t_gid);
-- Options
INSERT INTO pmgr_options (`name`) VALUES ('theme');
SET @t_oid = LAST_INSERT_ID();
INSERT INTO pmgr_options (`name`) VALUES ('menu');
SET @m_oid = LAST_INSERT_ID();
-- Default Option Values
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@t_oid, 'blue');
INSERT INTO pmgr_default_options (`option_value_id`) VALUES(LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@m_oid, 'basic');
INSERT INTO pmgr_default_options (`option_value_id`) VALUES(LAST_INSERT_ID());
-- Group options
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@t_oid, 'gold');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@o_gid, LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@t_oid, 'silver');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@a_gid, LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@t_oid, 'red');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@m_gid, LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@m_oid, 'advanced');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@o_gid, LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@m_oid, 'advanced');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@a_gid, LAST_INSERT_ID());
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@m_oid, 'restricted');
INSERT INTO pmgr_group_options (`group_id`, `option_value_id`)
VALUES(@t_gid, LAST_INSERT_ID());
-- User Options
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@m_oid, 'special');
INSERT INTO pmgr_user_options (`user_id`, `option_value_id`)
VALUES(@s_uid, LAST_INSERT_ID());
-- Site Options
INSERT INTO pmgr_option_values (`option_id`, `value`) VALUES (@t_oid, 'site-theme');
INSERT INTO pmgr_site_options (`site_id`, `option_value_id`)
VALUES(@f_sid, LAST_INSERT_ID());
-- SELECT U.id, P.name, MAX(P.access)
-- FROM pmgr_users U
-- LEFT JOIN pmgr_site_membership M ON M.user_id = U.id
-- LEFT JOIN pmgr_groups G ON G.id = M.group_id
-- LEFT JOIN pmgr_group_permissions P ON P.group_id = G.id
-- GROUP BY U.id, P.name
-- User access to site
SELECT U.id, U.login, COUNT(G.id) AS 'groups', MIN(G.rank) AS highest_rank
FROM pmgr_users U
JOIN pmgr_site_memberships M ON M.user_id = U.id
JOIN pmgr_sites S ON S.id = M.site_id
JOIN pmgr_groups G ON G.id = M.group_id
WHERE S.code = 'VSS'
GROUP BY U.id
-- User Options
SELECT O.id, O.name, O.default,
GROUP_CONCAT(Uopt.value) AS 'value', COUNT(U.id) AS 'count'
FROM pmgr_options O
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_id = O.id
LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
WHERE U.id = 1
GROUP BY O.id
-- Group Options
SELECT O.id, O.name, O.default,
GROUP_CONCAT(Gopt.value) AS 'value', COUNT(G.id) AS 'count'
FROM pmgr_options O
LEFT JOIN pmgr_group_options Gopt ON Gopt.option_id = O.id
LEFT JOIN pmgr_groups G ON G.id = Gopt.group_id
WHERE G.id = 1
GROUP BY O.id
-- Site Options
SELECT O.id, O.name, O.default,
GROUP_CONCAT(Sopt.value) AS 'value', COUNT(S.id) AS 'count'
FROM pmgr_options O
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_id = O.id
LEFT JOIN pmgr_sites S ON S.id = Sopt.site_id
WHERE S.id = 1
GROUP BY O.id
-- Option value for member & site
SELECT O.id, O.name, O.default,
S.id AS site_id, Sopt.value,
G.id AS group_id, Gopt.value,
U.id AS user_id, Uopt.value
FROM pmgr_options O
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_id = O.id
LEFT JOIN pmgr_sites S ON S.id = Sopt.site_id
LEFT JOIN pmgr_group_options Gopt ON Gopt.option_id = O.id
LEFT JOIN pmgr_groups G ON G.id = Gopt.group_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_id = O.id
LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
WHERE O.name = 'theme'
--GROUP BY O.id
-- Option value for member & site
-- 1) User
SET @sid = 1;
SET @uid = 1;
SET @oid = 1;
SELECT O.name, U.id, Uopt.value
FROM pmgr_options O
JOIN pmgr_user_options Uopt ON Uopt.option_id = O.id
JOIN pmgr_users U ON U.id = Uopt.user_id
-- JOIN pmgr_site_memberships M ON M.user_id = U.id
-- JOIN pmgr_groups G ON G.id = M.group_id
-- JOIN pmgr_sites S ON S.id = M.site_id
WHERE -- S.id = @sid AND
U.id = @uid AND O.id = @oid
;
-- 2) Group
SELECT O.name, G.rank, G.id, Gopt.value
FROM pmgr_options O
JOIN pmgr_group_options Gopt ON Gopt.option_id = O.id
JOIN pmgr_groups G ON G.id = Gopt.group_id
JOIN pmgr_site_memberships M ON M.group_id = G.id
JOIN pmgr_users U ON U.id = M.user_id
JOIN pmgr_sites S ON S.id = M.site_id
WHERE S.id = @sid AND U.id = @uid AND O.id = @oid
ORDER BY G.rank
;
-- 3) Site
SELECT O.name, S.id, Sopt.value
FROM pmgr_options O
JOIN pmgr_site_options Sopt ON Sopt.option_id = O.id
JOIN pmgr_sites S ON S.id = Sopt.site_id
-- JOIN pmgr_site_memberships M ON M.site_id = S.id
-- JOIN pmgr_groups G ON G.id = M.group_id
-- JOIN pmgr_users U ON U.id = M.user_id
WHERE S.id = @sid
-- AND U.id = @uid
AND O.id = @oid
;
-- 3) Default
SELECT O.name, O.default AS 'value'
FROM pmgr_options O
WHERE O.id = @oid
;
-- User Permissions
-- Group Permissions
-- All option values, in order
SELECT O.name, V.value,
U.id AS uid, G.id AS gid, S.id as sid,
Dopt.id AS did, G.rank
FROM pmgr_option_values V
JOIN pmgr_options O ON O.id = V.option_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_value_id = V.id
LEFT JOIN pmgr_group_options Gopt ON Gopt.option_value_id = V.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_value_id = V.id
LEFT JOIN pmgr_default_options Dopt ON Dopt.option_value_id = V.id
LEFT JOIN pmgr_groups G ON G.id = Gopt.group_id
LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
LEFT JOIN pmgr_sites S ON S.id = Sopt.site_id
WHERE O.id = @oid
ORDER BY IF(U.id IS NOT NULL, 1,
IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4))) ASC,
IF (G.id IS NOT NULL, G.rank, 0) ASC
-- Option values relevant to the user and site, in order
SELECT O.name, V.value,
U.id AS uid, G.id AS gid, S.id as sid,
Dopt.id AS did, G.rank
FROM pmgr_option_values V
JOIN pmgr_options O ON O.id = V.option_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_value_id = V.id
LEFT JOIN pmgr_group_options Gopt ON Gopt.option_value_id = V.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_value_id = V.id
LEFT JOIN pmgr_default_options Dopt ON Dopt.option_value_id = V.id
LEFT JOIN pmgr_groups G ON G.id = Gopt.group_id
LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
LEFT JOIN pmgr_sites S ON S.id = Sopt.site_id
JOIN pmgr_site_memberships M ON M.user_id = U.id AND M.site_id = S.id
WHERE S.id = @sid AND U.id = @uid AND O.id = @oid
ORDER BY IF(U.id IS NOT NULL, 1,
IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4))) ASC,
IF (G.id IS NOT NULL, G.rank, 0) ASC
SET @sid = 1;
SET @uid = 1;
SET @oid = 1;
SELECT O.name, V.value,
U.id AS uid,
-- G.id AS gid,
S.id as sid,
Dopt.id AS did
-- G.rank
FROM pmgr_option_values V
JOIN pmgr_options O ON O.id = V.option_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_value_id = V.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_value_id = V.id
-- LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
-- LEFT JOIN pmgr_group_options Gopt ON Gopt.option_value_id = V.id
LEFT JOIN pmgr_default_options Dopt ON Dopt.option_value_id = V.id
-- LEFT JOIN pmgr_groups G ON G.id = Gopt.group_id
LEFT JOIN pmgr_users U ON U.id = Uopt.user_id
LEFT JOIN pmgr_sites S ON S.id = Sopt.site_id
JOIN pmgr_site_memberships M ON M.user_id = U.id -- AND M.site_id = S.id
WHERE -- S.id = @sid AND U.id = @uid AND
O.id = @oid
ORDER BY IF(U.id IS NOT NULL, 1,
-- IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4)) -- ) ASC,
-- IF (G.id IS NOT NULL, G.rank, 0) ASC
-- ------------------------------------------------------------
-- ------------------------------------------------------------
-- ------------------------------------------------------------
-- Working version (without defaults)
SET @sid = 1;
SET @uid = 1;
SET @oid = 1;
SELECT O.name, O.id AS oid, V.value, V.id AS vid,
U.id AS uid,
G.id AS gid,
S.id AS sid,
-- Dopt.id AS did
G.rank
FROM pmgr_users U
JOIN pmgr_site_memberships M ON M.user_id = U.id
JOIN pmgr_sites S ON S.id = M.site_id
LEFT JOIN pmgr_groups G ON G.id = M.group_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.user_id = U.id
LEFT JOIN pmgr_group_options Gopt ON Gopt.group_id = G.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.site_id = S.id
LEFT JOIN pmgr_option_values V ON (V.id = Uopt.option_value_id OR
V.id = Gopt.option_value_id OR
V.id = Sopt.option_value_id)
JOIN pmgr_options O ON O.id = V.option_id
WHERE S.id = @sid AND U.id = @uid AND O.id = @oid
ORDER BY IF(U.id IS NOT NULL, 1,
IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4))) ASC,
IF (G.id IS NOT NULL, G.rank, 0) ASC
;
SET @sid = 1;
SET @uid = 1;
SET @oid = 1;
SELECT O.name, O.id AS oid, V.value, V.id AS vid,
U.id AS uid,
G.id AS gid,
S.id AS sid,
-- Dopt.id AS did
G.rank
FROM pmgr_options O
LEFT JOIN pmgr_option_values V ON V.option_id = O.id
-- Now have the option and all possible values
LEFT JOIN pmgr_user_options Uopt ON Uopt.option_value_id = V.id
LEFT JOIN pmgr_group_options Gopt ON Gopt.option_value_id = V.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.option_value_id = V.id
-- Now have the user/group/site that each value applies to
LEFT JOIN pmgr_users U U ON Uopt.user_id = U.id OR Uopt.user_id IS NULL
-- Now restricted to our user
JOIN pmgr_site_memberships M ON M.user_id = U.id
JOIN pmgr_sites S ON S.id = M.site_id
ON O.id = V.option_id
LEFT JOIN pmgr_groups G ON G.id = M.group_id
LEFT JOIN pmgr_option_values V ON (V.id = Uopt.option_value_id OR
V.id = Gopt.option_value_id OR
V.id = Sopt.option_value_id)
JOIN
WHERE S.id = @sid AND U.id = @uid AND O.id = @oid
ORDER BY IF(U.id IS NOT NULL, 1,
IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4))) ASC,
IF (G.id IS NOT NULL, G.rank, 0) ASC
;
SET @sid = 1;
SET @uid = 1;
SET @oid = 1;
SELECT O.name, O.id AS oid, V.value, V.id AS vid,
U.id AS uid,
G.id AS gid,
S.id AS sid,
-- Dopt.id AS did
G.rank
FROM pmgr_options O LEFT JOIN pmgr_option_values V ON V.option_id = O.id,
pmgr_users U
JOIN pmgr_site_memberships M ON M.user_id = U.id
JOIN pmgr_sites S ON S.id = M.site_id
LEFT JOIN pmgr_groups G ON G.id = M.group_id
LEFT JOIN pmgr_user_options Uopt ON Uopt.user_id = U.id
LEFT JOIN pmgr_group_options Gopt ON Gopt.group_id = G.id
LEFT JOIN pmgr_site_options Sopt ON Sopt.site_id = S.id,
WHERE S.id = @sid AND U.id = @uid AND O.id = @oid
AND (V.id = Uopt.option_value_id OR
V.id = Gopt.option_value_id OR
V.id = Sopt.option_value_id)
ORDER BY IF(U.id IS NOT NULL, 1,
IF (G.id IS NOT NULL, 2,
IF (S.id IS NOT NULL, 3, 4))) ASC,
IF (G.id IS NOT NULL, G.rank, 0) ASC
;