PageRenderTime 127ms CodeModel.GetById 119ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/models/m_acl.erl

https://code.google.com/p/zotonic/
Erlang | 64 lines | 29 code | 10 blank | 25 comment | 6 complexity | b29f870a598f1158c2763006cb431ae1 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%% Date: 2009-04-27
 4%%
 5%% @doc Template access for access control functions and state
 6
 7%% Copyright 2009 Marc Worrell
 8%%
 9%% Licensed under the Apache License, Version 2.0 (the "License");
10%% you may not use this file except in compliance with the License.
11%% You may obtain a copy of the License at
12%% 
13%%     http://www.apache.org/licenses/LICENSE-2.0
14%% 
15%% Unless required by applicable law or agreed to in writing, software
16%% distributed under the License is distributed on an "AS IS" BASIS,
17%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18%% See the License for the specific language governing permissions and
19%% limitations under the License.
20
21-module(m_acl).
22-author("Marc Worrell <marc@worrell.nl").
23
24-behaviour(gen_model).
25
26%% interface functions
27-export([
28    m_find_value/3,
29    m_to_list/2,
30    m_value/2
31]).
32
33-include_lib("zotonic.hrl").
34
35
36%% @doc Fetch the value for the key from a model source
37%% @spec m_find_value(Key, Source, Context) -> term()
38m_find_value(user, #m{value=undefined}, Context) ->
39    z_acl:user(Context);
40m_find_value(is_admin, #m{value=undefined}, Context) ->
41    z_acl:is_allowed(use, mod_admin_config, Context);
42m_find_value(Action, #m{value=undefined} = M, _Context) 
43    when Action == use orelse Action == admin orelse Action == view
44    orelse Action == delete orelse Action == update orelse Action == insert ->
45    M#m{value={is_allowed, Action}};
46m_find_value(is_allowed, #m{value=undefined} = M, _Context) ->
47    M#m{value=is_allowed};
48m_find_value(Action, #m{value=is_allowed} = M, _Context) ->
49    M#m{value={is_allowed, Action}};
50m_find_value(Object, #m{value={is_allowed, Action}}, Context) when is_binary(Object) ->
51    z_acl:is_allowed(Action, z_convert:to_atom(Object), Context);
52m_find_value(Object, #m{value={is_allowed, Action}}, Context) ->
53    z_acl:is_allowed(Action, Object, Context).
54
55
56%% @doc Transform a m_config value to a list, used for template loops
57%% @spec m_to_list(Source, Context) -> List
58m_to_list(_, _Context) ->
59    [].
60
61%% @doc Transform a model value so that it can be formatted or piped through filters
62%% @spec m_value(Source, Context) -> term()
63m_value(#m{value=undefined}, _Context) ->
64    undefined.