-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_acl_simple_roles.erl
496 lines (444 loc) · 19.8 KB
/
mod_acl_simple_roles.erl
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
%% @author Marc Worrell <[email protected]>
%% @copyright 2010 Marc Worrell
%% Date: 2010-05-05
%% @doc Simple ACL module based on roles.
%% Copyright 2010 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(mod_acl_simple_roles).
-author("Marc Worrell <[email protected]>").
-mod_title("ACL Simple Roles").
-mod_description("Simple role based access control. Use this for a site with different editor roles.").
-mod_prio(500).
-mod_schema(1).
-mod_depends([]).
-mod_provides([acl]).
%% interface functions
-export([
manage_schema/2,
observe_acl_is_allowed/2,
observe_acl_can_see/2,
observe_acl_logon/2,
observe_acl_logoff/2,
observe_acl_rsc_update_check/3,
observe_acl_add_sql_check/2,
observe_rsc_update/3,
observe_admin_menu/3
]).
-include("zotonic.hrl").
-include_lib("modules/mod_admin/include/admin_menu.hrl").
-record(acl_user, {modules, categories, roles, view_all, only_update_own, file_mimes, file_size, visible_for}).
-define(ROLE_MEMBER, role_member).
%% @doc Check if the user is allowed to perform Action on Object
%% @todo #acl_edge
observe_acl_is_allowed(#acl_is_allowed{object=undefined}, _Context) ->
undefined;
observe_acl_is_allowed(#acl_is_allowed{action=view, object=Id}, #context{user_id=undefined} = Context) ->
is_view_public(Id, Context);
observe_acl_is_allowed(#acl_is_allowed{}, #context{user_id=undefined}) ->
undefined;
% Logged on users
observe_acl_is_allowed(#acl_is_allowed{action=view, object=Id}, Context) ->
can_view(Id, Context);
observe_acl_is_allowed(#acl_is_allowed{action=insert, object=#acl_media{mime=Mime, size=Size}}, Context) ->
can_media(Mime, Size, Context);
observe_acl_is_allowed(#acl_is_allowed{action=insert, object=#acl_rsc{category=Cat}}, Context) ->
can_insert(Cat, Context);
observe_acl_is_allowed(#acl_is_allowed{action=insert, object=Cat}, Context) when is_atom(Cat) ->
can_insert(Cat, Context);
observe_acl_is_allowed(#acl_is_allowed{action=update, object=Id}, Context) ->
case m_rsc:p_no_acl(Id, is_authoritative, Context) of
true -> can_edit(Id, Context);
_ -> undefined
end;
observe_acl_is_allowed(#acl_is_allowed{object=#acl_edge{} = Edge}, Context) ->
can_edge(Edge, Context);
observe_acl_is_allowed(#acl_is_allowed{action=delete, object=Id}, Context) ->
can_edit(Id, Context);
observe_acl_is_allowed(#acl_is_allowed{action=Action, object=ModuleName}, Context) when Action == use; Action == admin ->
can_module(Action, ModuleName, Context);
observe_acl_is_allowed(#acl_is_allowed{}, _Context) ->
undefined.
%% @doc Return the max visible_for an user can see, used for pruning during searches
observe_acl_can_see(#acl_can_see{}, #context{user_id=undefined}) ->
?ACL_VIS_PUBLIC;
observe_acl_can_see(#acl_can_see{}, _Context) ->
?ACL_VIS_USER.
%% @doc Let the user log on, this is the moment to start caching information.
observe_acl_logon(#acl_logon{id=UserId}, Context) ->
logon(UserId, Context).
%% @doc Let the user log off, clean up any cached information.
observe_acl_logoff(#acl_logoff{}, Context) ->
Context#context{acl=undefined, user_id=undefined}.
%% @doc Filter the properties before an update. Return filtered/updated resource proplist or
%% the tuple {error, Reason}
observe_acl_rsc_update_check(#acl_rsc_update_check{}, {error, Reason}, _Context) ->
{error, Reason};
observe_acl_rsc_update_check(#acl_rsc_update_check{id=insert_rsc}, Props, Context) ->
PropsPubl = case proplists:get_value(is_published, Props) of
undefined -> z_utils:prop_replace(is_published, false, Props);
_ -> Props
end,
PropsVis = constrain_visible_for(insert_rsc, PropsPubl, Context),
case proplists:get_value(is_authoritative, PropsVis) of
undefined -> z_utils:prop_replace(is_authoritative, true, PropsVis);
_ -> PropsVis
end;
observe_acl_rsc_update_check(#acl_rsc_update_check{id=Id}, Props, Context) ->
constrain_visible_for(Id, Props, Context).
% Make sure that the user doesn't try to change the visibility beyond what that user is allowed to do.
% Retain any visibility the rsc already had.
constrain_visible_for(Id, Props, Context) ->
case proplists:get_value(visible_for, Props) of
undefined ->
case min_visible(Context) of
N when is_integer(N) andalso Id =:= insert_rsc ->
z_utils:prop_replace(visible_for, N, Props);
_ ->
Props
end;
Vis ->
CurrVis = case Id of
insert_rsc -> ?ACL_VIS_USER;
_ -> m_rsc:p_no_acl(Id, visible_for, Context)
end,
NewVis = z_convert:to_integer(Vis),
case NewVis < CurrVis of
true ->
case min_visible(Context) of
N when is_integer(N) ->
z_utils:prop_replace(visible_for, erlang:max(N, NewVis), Props);
undefined ->
Props
end;
false ->
Props
end
end.
min_visible(#context{user_id=?ACL_ADMIN_USER_ID}) ->
?ACL_VIS_PUBLIC;
min_visible(#context{acl=admin}) ->
?ACL_VIS_PUBLIC;
min_visible(#context{acl=ACL}) ->
case ACL of
#acl_user{visible_for=VisFor} -> VisFor;
_ -> ?ACL_VIS_USER
end.
%% @doc Restrict what an user can see in z_search SQL queries.
observe_acl_add_sql_check(#acl_add_sql_check{alias=Alias, args=Args}, #context{ user_id = undefined }) ->
% Restrict anonymous users to published content with public visible_for level
{[
Alias, ".is_published = true and "
, Alias, ".publication_start <= now() and "
, Alias, ".publication_end >= now() and "
, Alias, ".visible_for = ", integer_to_list(?ACL_VIS_PUBLIC)
], Args};
observe_acl_add_sql_check(#acl_add_sql_check{args=Args}, #context{ acl = admin }) ->
% No restrictions for sudo users
{[], Args};
observe_acl_add_sql_check(#acl_add_sql_check{args=Args}, #context{ acl = #acl_user{ view_all = true } }) ->
% No restrictions for 'view all' users
{[], Args};
observe_acl_add_sql_check(#acl_add_sql_check{alias=Alias, args=Args, search_sql=SearchSql}, #context{ user_id = UserId } = Context) ->
case can_module(use, mod_admin, Context) of
true ->
% No restrictions for admin users
{[], Args};
_ ->
#search_sql{ extra = Extra } = SearchSql,
case lists:member(no_publish_check, Extra) of
true ->
{[], Args};
false ->
% Limit signed in users to community content or content
% created by the user.
{[
"(",
"("
, Alias, ".visible_for = any(array[0,1,2]::int[]) and "
, Alias, ".is_published = true and "
, Alias, ".publication_start <= now() and "
, Alias, ".publication_end >= now()"
")",
" or ", Alias, ".creator_id = ", integer_to_list(UserId),
" or ", Alias, ".id = ", integer_to_list(UserId),
")"
], Args}
end
end.
%% @doc Check if the update contains information for a acl role. If so then modify the acl role
%% information so that it is easier to handle.
%% @spec observe_rsc_update({rsc_update, ResourceId, OldResourceProps}, {Changed, UpdateProps}, Context) -> {NewChanged, NewUpdateProps}
observe_rsc_update(#rsc_update{}, {Changed, Props}, _Context) ->
case proplists:is_defined(acl_cat, Props)
orelse proplists:is_defined(acl_mod, Props)
orelse proplists:is_defined(acl_mime, Props)
orelse proplists:is_defined(acl_view_all, Props)
orelse proplists:is_defined(acl_file_upload_size, Props)
orelse proplists:is_defined(acl_only_update_own, Props) of
true ->
Cats = proplists:get_all_values(acl_cat, Props),
Mods = proplists:get_all_values(acl_mod, Props),
Mimes = proplists:get_all_values(acl_mime, Props),
VisibleFor = z_convert:to_integer(proplists:get_value(acl_visible_for, Props, 0)),
FileSize = z_convert:to_integer(proplists:get_value(acl_file_upload_size, Props, 0)),
ReadAll = z_convert:to_bool(proplists:get_value(acl_view_all, Props, false)),
OnlyOwn = z_convert:to_bool(proplists:get_value(acl_only_update_own, Props, false)),
Cats1 = [ z_convert:to_atom(C) || C <- Cats, C /= <<>> ],
Mods1 = [ z_convert:to_atom(M) || M <- Mods, M /= <<>> ],
Mimes1 = [ z_convert:to_binary(M) || M <- Mimes, M /= <<>> ],
Props1 = proplists:delete(acl_cat,
proplists:delete(acl_mod,
proplists:delete(acl_file_upload_size,
proplists:delete(acl_view_all,
proplists:delete(acl_visible_for,
proplists:delete(acl_only_update_own,
proplists:delete(acl_mime, Props))))))),
Props2 = [{acl, [ {categories, Cats1},
{modules, Mods1},
{view_all,ReadAll},
{only_update_own, OnlyOwn},
{file_upload_size, FileSize},
{file_mime, Mimes1},
{visible_for, VisibleFor}
]} | Props1],
{true, Props2};
false ->
{Changed, Props}
end.
%% @doc On logon, cache ACL information in the context record
%% @todo Cache this information in the session (oid) so that we don't need to refetch it
%% on every request (each request is a continuation with a new logon).
logon(UserId, Context) ->
% Fetch roles the user is member of
ContextAdmin = z_acl:sudo(Context),
case m_edge:subjects(UserId, acl_role_member, ContextAdmin) of
[] ->
%% When not member of a role then fallback to the member or anonymous role.
case m_rsc:name_to_id(?ROLE_MEMBER, ContextAdmin) of
{ok, RoleMember} -> logon_roles(UserId, [RoleMember], Context, ContextAdmin);
{error, _} -> logon_roles(UserId, [], Context, ContextAdmin)
end;
Roles ->
logon_roles(UserId, Roles, Context, ContextAdmin)
end.
logon_roles(UserId, Roles, Context, ContextAdmin) ->
RolesFiltered = [ R || R <- Roles,
m_rsc:is_a(R, acl_role, ContextAdmin),
m_rsc:p_no_acl(R, is_published, ContextAdmin) ],
% Merge all role's categories and modules
ACLs = [ m_rsc:p_no_acl(R, acl, ContextAdmin) || R <- RolesFiltered ],
{Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor}
= combine(ACLs, [], [], false, undefined, 0, [], 3),
Context#context{user_id=UserId,
acl=#acl_user{categories=lists:flatten(Cats),
modules=lists:flatten(Mods),
roles=RolesFiltered,
view_all=ViewAll,
only_update_own=OnlyOwn,
file_size=FileSize,
file_mimes=lists:flatten(FileMime),
visible_for=VisibleFor}}.
combine([], Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor) ->
{Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor};
combine([undefined|Rest], Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor) ->
combine(Rest, Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor);
combine([ACL|Rest], Cats, Mods, ViewAll, OnlyOwn, FileSize, FileMime, VisibleFor) ->
OnlyOwn1 = case {OnlyOwn, proplists:get_value(only_update_own, ACL, false)} of
{undefined, UpdOwn} -> UpdOwn;
{true, true} -> true;
{_, _} -> false
end,
combine(Rest,
[proplists:get_value(categories, ACL, [])|Cats],
[proplists:get_value(modules, ACL, [])|Mods],
ViewAll orelse proplists:get_value(view_all, ACL, false),
OnlyOwn1,
erlang:max(proplists:get_value(file_upload_size, ACL, 0), FileSize),
[proplists:get_value(file_mime, ACL, [])|FileMime],
erlang:min(proplists:get_value(visible_for, ACL, 0), VisibleFor)).
%% @doc Check if an user can see something (only called for authenticated users)
can_view(Id, Context) ->
case can_view_all(Context) == true
orelse can_edit(Id, Context) == true
orelse is_view_community(Id, Context) == true of
true -> true;
false -> undefined
end.
%% @doc Check if the user has 'view_all' permission
can_view_all(#context{user_id=?ACL_ADMIN_USER_ID}) ->
true;
can_view_all(#context{acl=undefined}) ->
undefined;
can_view_all(#context{acl=Acl}) ->
case Acl#acl_user.view_all of
true -> true;
_ -> undefined
end.
%% @doc Check if the user can edit a rsc id
can_edit(_Id, #context{user_id=?ACL_ADMIN_USER_ID}) ->
true;
can_edit(_Id, #context{acl=undefined}) ->
undefined;
%% @doc A user can edit himself
can_edit(UserId, #context{user_id=UserId, acl=#acl_user{only_update_own=true}}) when UserId /= undefined ->
true;
%% @doc A user can edit content he created
can_edit(Id, #context{user_id=UserId, acl=#acl_user{only_update_own=true}} = Context) when UserId /= undefined ->
case m_rsc:p_no_acl(Id, creator_id, Context) of
UserId -> true;
_ -> undefined
end;
can_edit(Id, #context{acl=Acl} = Context) ->
IsA = m_rsc:p_no_acl(Id, is_a, Context),
can_edit1(IsA, Acl#acl_user.categories).
can_edit1(undefined, _Allowed) -> undefined;
can_edit1([], _Allowed) -> undefined;
can_edit1([{Cat,_}|Cats], Allowed) ->
case lists:member(Cat, Allowed) of
true -> true;
false -> can_edit1(Cats, Allowed)
end.
%% @doc Check if the user can use a module
can_module(_Action, _Module, #context{user_id=?ACL_ADMIN_USER_ID}) ->
true;
can_module(_Action, _Module, #context{acl=undefined}) ->
undefined;
can_module(use, Module, #context{acl=Acl}) ->
case lists:member(Module, Acl#acl_user.modules) of
true -> true;
false -> undefined
end;
can_module(_Action, _Module, _Context) ->
undefined.
%% @doc Check if the category (or one of its parents) is in the category access list of the user
can_insert(_Cat, #context{user_id=?ACL_ADMIN_USER_ID}) ->
true;
can_insert(_Cat, #context{acl=undefined}) ->
undefined;
can_insert(Cat, #context{acl=Acl} = Context) ->
case lists:member(Cat, Acl#acl_user.categories) of
true ->
true;
false ->
IsA = m_category:is_a(Cat, Context),
can_insert1(IsA, Acl#acl_user.categories)
end.
can_insert1([], _Allowed) ->
undefined;
can_insert1([Cat|Cats], Allowed) ->
case lists:member(Cat, Allowed) of
true -> true;
false -> can_insert1(Cats, Allowed)
end.
%% @doc Check if a rsc is public viewable
is_view_public(Id, Context) ->
is_view_level(Id, 0, Context).
is_view_community(Id, Context) ->
is_view_level(Id, 1, Context).
is_view_level(Id, Level, Context) ->
Acl = m_rsc:get_acl_props(Id, Context),
case Acl#acl_props.is_published of
false ->
false;
true ->
case Acl#acl_props.visible_for =< Level of
false ->
false;
true ->
Date = calendar:universal_time(),
Acl#acl_props.publication_start =< Date andalso Acl#acl_props.publication_end >= Date
end
end.
%% @doc Check if the user has edit permission on a edge. The user must be able to edit the subject and view the object.
can_edge(_, #context{user_id=?ACL_ADMIN_USER_ID}) ->
true;
can_edge(#acl_edge{predicate=acl_role_member, subject_id=SubjectId, object_id=ObjectId}, Context) ->
case can_insert(acl_role, Context) == true
andalso can_edit(SubjectId, Context) == true
andalso can_view(ObjectId, Context) == true of
true -> true;
false -> undefined
end;
can_edge(#acl_edge{subject_id=SubjectId, object_id=ObjectId}, Context) ->
case can_edit(SubjectId, Context) == true
andalso can_view(ObjectId, Context) == true of
true -> true;
false -> undefined
end.
can_media(Mime, Size, Context) when is_list(Mime) ->
can_media(z_convert:to_binary(Mime), Size, Context);
can_media(Mime, Size, #context{acl=ACL}) ->
case ACL of
#acl_user{file_size=MaxSize, file_mimes=Allowed} ->
case Size =< MaxSize * 1024 of
true ->
case lists:member(<<"*/*">>, Allowed)
orelse lists:member(Mime, Allowed) of
true -> true;
false ->
case lists:member(make_wildcard(Mime), Allowed) of
true -> true;
false -> undefined
end
end;
false ->
undefined
end;
_ -> undefined
end.
make_wildcard(<<"text/html-video-embed">>) ->
<<"video/*">>;
make_wildcard(<<"text/html-oembed">>) ->
<<"video/*">>;
make_wildcard(Mime) ->
[Type|_] = string:tokens(z_convert:to_list(Mime), "/"),
list_to_binary(Type ++ "/*").
observe_admin_menu(admin_menu, Acc, Context) ->
[
#menu_item{id=admin_acl,
parent=admin_auth,
label=?__("Access Control", Context),
url={admin_acl},
visiblecheck={acl, use, ?MODULE}}
|Acc].
%% @doc The datamodel for the role based ACL
manage_schema(install, _Context) ->
#datamodel{categories=
[
{acl_role,
meta,
[{title, <<"ACL Role">>}]}
],
predicates=
[{acl_role_member,
[{title, <<"ACL Role Member">>}],
[{acl_role, person}]
}],
resources=
[
{?ROLE_MEMBER, acl_role,
[{visible_for, 1},
{title, "ACL role for members"},
{summary, "The rights of this role are assigned to members (logged on) when they are not "
"member of any other ACL role. Make the user member of another role to overrule this role."},
{acl, [ {view_all, false},
{only_update_own, false},
{file_upload_size, 1024},
{file_mime,["image/jpeg", "image/png", "image/gif"]},
{categories,[article,image]},
{modules,[]}]}
]
}
]
}.