PageRenderTime 14ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/controllers/role_controller.erl

http://github.com/AF83/ucengine
Erlang | 121 lines | 87 code | 17 blank | 17 comment | 0 complexity | 37648af5ed9187e3938ce733559542ba 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(role_controller).
  19. -export([init/0, add/4, delete/4, add_access/4, delete_access/4]).
  20. -include("uce.hrl").
  21. init() ->
  22. [#uce_route{method='POST',
  23. path=["role"],
  24. callback={?MODULE, add,
  25. [{"uid", required, string},
  26. {"sid", required, string},
  27. {"name", required, string}]}},
  28. #uce_route{method='DELETE',
  29. path=["role", name],
  30. callback={?MODULE, delete,
  31. [{"uid", required, string},
  32. {"sid", required, string}]}},
  33. #uce_route{method='POST',
  34. path=["role", name, "acl"],
  35. callback={?MODULE, add_access,
  36. [{"uid", required, string},
  37. {"sid", required, string},
  38. {"object", "all", string},
  39. {"action", "all", string},
  40. {"conditions", [], dictionary}]}},
  41. #uce_route{method='DELETE',
  42. path=["role", name, "acl", object, action],
  43. callback={?MODULE, delete_access,
  44. [{"uid", required, string},
  45. {"sid", required, string},
  46. {"conditions", [], dictionary}]}}].
  47. add(Domain, [], [Uid, Sid, Name], _) ->
  48. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  49. {ok, true} = uce_access:assert(Domain, Uid, "", "role", "add", [{"name", Name}]),
  50. {ok, created} = uce_role:add(Domain, #uce_role{id=Name}),
  51. {ok, _} = uce_event:add(Domain, #uce_event{id=none,
  52. from=Uid,
  53. location="",
  54. type="internal.role.add",
  55. metadata=[{"name", Name}]}),
  56. json_helpers:created(Domain).
  57. delete(Domain, [{name, Name}], [Uid, Sid], _) ->
  58. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  59. {ok, true} = uce_access:assert(Domain, Uid, "", "role", "delete", [{"name", Name}]),
  60. {ok, _} = uce_event:add(Domain, #uce_event{id=none,
  61. from=Uid,
  62. location="",
  63. type="internal.role.delete",
  64. metadata=[{"name", Name}]}),
  65. {ok, deleted} = uce_role:delete(Domain, Name),
  66. json_helpers:ok(Domain).
  67. add_access(Domain, [{name, Role}], [Uid, Sid, Object, Action, Conditions], _) ->
  68. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  69. {ok, true} = uce_access:assert(Domain, Uid, "",
  70. "access", "add", [{"role", Role},
  71. {"object", Object},
  72. {"action", Action}]),
  73. {ok, updated} = uce_role:add_access(Domain, Role,
  74. #uce_access{object=Object,
  75. action=Action,
  76. conditions=Conditions}),
  77. {ok, _} = uce_event:add(Domain, #uce_event{id=none,
  78. from=Uid,
  79. location="",
  80. type="internal.access.add",
  81. metadata=[{"role", Role},
  82. {"action", Action},
  83. {"object", Object}] ++
  84. Conditions}),
  85. json_helpers:ok(Domain).
  86. delete_access(Domain, [{name, Role}, {object, Object}, {action, Action}], [Uid, Sid, Conditions], _) ->
  87. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  88. {ok, true} = uce_access:assert(Domain, Uid, "",
  89. "access", "add", [{"role", Role},
  90. {"object", Object},
  91. {"action", Action}]),
  92. {ok, updated} = uce_role:delete_access(Domain, Role,
  93. #uce_access{object=Object,
  94. action=Action,
  95. conditions=Conditions}),
  96. {ok, _} = uce_event:add(Domain, #uce_event{id=none,
  97. from=Uid,
  98. location="",
  99. type="internal.access.delete",
  100. metadata=[{"role", Role},
  101. {"action", Action},
  102. {"object", Object}] ++
  103. Conditions}),
  104. json_helpers:ok(Domain).