Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
SELECT
    theAro.model AS RequesterModel,
    theAro.foreign_key AS RequesterId,
    theAco.model AS ControlModel,
    theAco.foreign_key AS ControlId,
    permissions._create AS CanCreate,  
    permissions._read AS CanRead,  
    permissions._update AS CanEdit,
    permissions._delete AS CanDelete   
FROM
    acos AS theAco
INNER JOIN
    acos AS ruleAco ON (
        ruleAco.lft <= theAco.lft AND ruleAco.rght >= theAco.rght)
INNER JOIN
    aros_acos AS permissions ON (
        permissions.aco_id = ruleAco.id)
INNER JOIN
    aros AS ruleAro ON (
        permissions.aro_id = ruleAro.id)
INNER JOIN
    aros AS theAro ON (
        theAro.lft >= ruleAro.lft AND theAro.rght <= ruleAro.rght)
WHERE
    theAro.model = "$string" AND
    theAro.foreign_key = $int AND
    permissions._update != 0 /* AND
    NOT EXISTS ( similar query, checking that the found rule is the bottom rule) */