PageRenderTime 17ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/ucengine/src/models/uce_role.erl

http://github.com/AF83/ucengine
Erlang | 175 lines | 128 code | 24 blank | 23 comment | 0 complexity | d977a1618683554b4be24e9f8b707081 MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(uce_role).
  19. -export([add/2,
  20. delete/2,
  21. update/2,
  22. get/2,
  23. exists/2,
  24. acl/2,
  25. add_access/3,
  26. delete_access/3,
  27. drop/1]).
  28. -include("uce.hrl").
  29. %
  30. % Public api
  31. %
  32. -spec add(domain(), role()) -> {ok, created} | erlang:throw({error, conflict}).
  33. add(Domain, #uce_role{id=Id} = Role) ->
  34. case exists(Domain, Id) of
  35. true ->
  36. throw({error, conflict});
  37. false ->
  38. internal_add(Domain, Role)
  39. end.
  40. -spec update(domain(), role()) -> {ok, updated} | erlang:throw({error, not_found}).
  41. update(Domain, #uce_role{id=Id} = Role) ->
  42. case exists(Domain, Id) of
  43. true ->
  44. internal_update(Domain, Role);
  45. false ->
  46. throw({error, not_found})
  47. end.
  48. -spec delete(domain(), role_id()) -> {ok, deleted} | erlang:throw({error, not_found}).
  49. delete(Domain, Id) ->
  50. case exists(Domain, Id) of
  51. true ->
  52. internal_delete(Domain, Id);
  53. false ->
  54. throw({error, not_found})
  55. end.
  56. -spec get(domain(), role_id()) -> {ok, role()} | erlang:throw({error, not_found}).
  57. get(Domain, Id) ->
  58. internal_get(Domain, Id).
  59. -spec exists(domain(), role_id()) -> true | false | erlang:throw({error, atom()}).
  60. exists(Domain, Id) ->
  61. case catch get(Domain, Id) of
  62. {error, not_found} ->
  63. false;
  64. {error, Reason} ->
  65. throw({error, Reason});
  66. _ ->
  67. true
  68. end.
  69. -spec acl(domain(), role_id()) -> {ok, updated} | erlang:throw({error, not_found}).
  70. acl(Domain, Id) ->
  71. {ok, Role} = get(Domain, Id),
  72. {ok, Role#uce_role.acl}.
  73. -spec add_access(domain(), role_id(), access()) -> {ok, updated} | erlang:throw({error, not_found}).
  74. add_access(Domain, Id, #uce_access{} = Access) ->
  75. {ok, Role} = get(Domain, Id),
  76. case uce_access:exists(Access, Role#uce_role.acl) of
  77. true ->
  78. {ok, updated};
  79. false ->
  80. update(Domain, Role#uce_role{acl=(Role#uce_role.acl ++ [Access])})
  81. end.
  82. -spec delete_access(domain(), role_id(), access()) -> {ok, updated} | erlang:throw({error, not_found}).
  83. delete_access(Domain, Id, #uce_access{} = Access) ->
  84. {ok, Role} = get(Domain, Id),
  85. ACL = case uce_access:exists(Access, Role#uce_role.acl) of
  86. true ->
  87. uce_access:delete(Access, Role#uce_role.acl);
  88. false ->
  89. Role#uce_role.acl
  90. end,
  91. update(Domain, Role#uce_role{acl=ACL}).
  92. -spec drop(domain()) -> true.
  93. drop(_Domain) ->
  94. delete_table().
  95. %
  96. % Private functions
  97. %
  98. -define(TAB, uce_role_cache).
  99. init_table() ->
  100. case ets:info(?TAB) of
  101. undefined ->
  102. ets:new(?TAB, [set, public, {keypos, 1}, named_table]);
  103. _ ->
  104. ok
  105. end.
  106. delete_table() ->
  107. case ets:info(?TAB) of
  108. undefined ->
  109. ok;
  110. _ ->
  111. ets:delete(?TAB)
  112. end.
  113. cache_add(Domain, #uce_role{id=Id} = Role) ->
  114. ets:insert(?TAB, {{Domain, Id}, Role}).
  115. cache_update(Domain, Role) ->
  116. cache_add(Domain, Role).
  117. cache_delete(Domain, Id) ->
  118. ets:delete(?TAB, {Domain, Id}).
  119. cache_get(Domain, Id) ->
  120. case ets:lookup(?TAB, {Domain, Id}) of
  121. [] ->
  122. undefined;
  123. [{{Domain, Id}, Role}] ->
  124. Role
  125. end.
  126. internal_add(Domain, Role) ->
  127. init_table(),
  128. cache_add(Domain, Role),
  129. (db:get(?MODULE, Domain)):add(Domain, Role).
  130. internal_update(Domain, Role) ->
  131. init_table(),
  132. cache_update(Domain, Role),
  133. (db:get(?MODULE, Domain)):update(Domain, Role).
  134. internal_delete(Domain, Id) ->
  135. init_table(),
  136. cache_delete(Domain, Id),
  137. (db:get(?MODULE, Domain)):delete(Domain, Id).
  138. internal_get(Domain, Id) ->
  139. init_table(),
  140. case cache_get(Domain, Id) of
  141. undefined ->
  142. case (db:get(?MODULE, Domain)):get(Domain, Id) of
  143. {ok, Role} ->
  144. cache_add(Domain, Role),
  145. {ok, Role};
  146. Error ->
  147. Error
  148. end;
  149. Role ->
  150. {ok, Role}
  151. end.