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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
define_tag(
'groupassignpath',
-namespace='admin_',
-req='group',
-req='path',
-opt='perms',
-priority='replace',
-description='Assigns the given path to the given group.'
);
// validate path
local('pathID' = 0);
iterate(admin_allowedfileroots, local('i'));
if(#i->second == #path);
#pathID = #i->first;
loop_abort;
/if;
/iterate;
!#pathID ? return(false);
// if no permissions are specified, default to all
local('columns') = array(
'create',
'read',
'write',
'move',
'copy',
'delete',
'inspect',
'any_extension'
);
!local_defined('perms') ? local('perms') = #columns;
// wrap all inlines in a single connection
inline( -database='lasso_internal', -sql='SELECT 1');
// retrieve the group ID
local('sql' = '
SELECT id
FROM security_groups
WHERE name = \'' + #group + '\'
');
inline( -sql=#sql);
local('groupID') = field('id');
/inline;
// look for an existing record
local('sql' = '
SELECT id
FROM security_group_file_perms
WHERE id_group = ' + #groupID + '
AND allow_root = ' + #pathID + '
');
protect;
inline( -sql=#sql);
// if one exists, update it
if(found_count);
local('updateperms' = string);
iterate(#columns, local('i'));
#updateperms += 'allow_' + #i + ' = \'' + (#perms >> #i ? 'Y' | 'N') + '\', ';
/iterate;
#updateperms->removetrailing(', ');
local('sql' = '
UPDATE security_group_file_perms
SET ' + #updateperms + '
WHERE id = ' + field('id') + '
');
inline( -sql=#sql); /inline;
// otherwise, insert a new one
else;
local(
'insertvalues' = string,
'insertcolumns' = string
);
iterate(#columns, local('i'));
#insertvalues += '\'' + (#perms >> #i ? 'Y' | 'N') + '\',\n';
#insertcolumns += 'allow_' + #i + ',\n';
/iterate;
#insertvalues->removetrailing(',\n');
#insertcolumns->removetrailing(',\n');
local('sql' = '
INSERT INTO security_group_file_perms (
id_group,
allow_root,
' + #insertcolumns + '
) VALUES (
' + #groupID + ',
' + #pathID + ',
' + #insertvalues + '
)
');
inline( -sql=#sql); /inline;
/if;
/inline;
return(true);
handle_error;
return(false);
/handle_error;
/protect;
/inline;
/define_tag;
|