PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/core/uce_vhost.erl

http://github.com/AF83/ucengine
Erlang | 180 lines | 129 code | 25 blank | 26 comment | 0 complexity | 6e7d508f13ac71ba9f1b25e66ec43da7 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_vhost).
  19. -behaviour(gen_server).
  20. -include("uce.hrl").
  21. -export([start_link/1, add_user/2]).
  22. -export([init/1,
  23. code_change/3,
  24. handle_call/3,
  25. handle_cast/2,
  26. handle_info/2,
  27. terminate/2]).
  28. %%
  29. %% Public api
  30. %%
  31. start_link(Domain) ->
  32. gen_server:start_link({local, uce_vhost_sup:name(Domain, "vhost")}, ?MODULE, [Domain], []).
  33. add_user(Domain, Uid) ->
  34. gen_server:call(uce_vhost_sup:name(Domain, "vhost"), {add_user, Uid}).
  35. %%
  36. %% gen_server callbacks
  37. %%
  38. init([Domain]) ->
  39. setup_db(Domain),
  40. setup_roles(Domain),
  41. setup_root_role(Domain),
  42. setup_bricks(Domain),
  43. setup_admin(Domain),
  44. setup_server(Domain),
  45. {ok, Domain}.
  46. handle_call({add_user, Uid}, _From, Domain) ->
  47. {ok, User} = uce_user:get(Domain, Uid),
  48. case uce_vhost_user_sup:start_child(Domain, [Domain, User]) of
  49. {ok, Pid} ->
  50. {reply, {ok, Pid}, Domain};
  51. {error, Reason} ->
  52. case gproc:lookup_local_name({Domain, uid, Uid}) of
  53. undefined ->
  54. {error, Reason};
  55. Pid ->
  56. {reply, {ok, Pid}, Domain}
  57. end
  58. end.
  59. handle_cast(_, State) ->
  60. {noreply, State}.
  61. handle_info(_Info, State) ->
  62. {reply, State}.
  63. code_change(_,State,_) ->
  64. {ok, State}.
  65. terminate(_Reason, _State) ->
  66. ok.
  67. %%
  68. %% Private functions
  69. %%
  70. setup_db(Domain) ->
  71. DBBackend = config:get(Domain, db),
  72. DBConfig = config:get(Domain, DBBackend),
  73. DBBackendModule = list_to_atom(lists:concat([DBBackend, "_db"])),
  74. DBBackendModule:init(Domain, DBConfig).
  75. setup_roles(Domain) ->
  76. case catch uce_role:add(Domain, #uce_role{id="default", acl=[]}) of
  77. {ok, created} ->
  78. ok;
  79. {error, conflict} ->
  80. ok;
  81. {error, Reason} ->
  82. throw({error, Reason})
  83. end,
  84. setup_role(Domain, config:get(Domain, roles)).
  85. setup_role(_, undefined) ->
  86. ok;
  87. setup_role(_, []) ->
  88. ok;
  89. setup_role(Domain, [{Name, ConfigACL}|Tail]) ->
  90. ACL = lists:map(fun({Action, Object, Conditions}) ->
  91. #uce_access{action=Action,
  92. object=Object,
  93. conditions=Conditions}
  94. end,
  95. ConfigACL),
  96. case catch uce_role:add(Domain, #uce_role{id=Name, acl=ACL}) of
  97. {ok, created} ->
  98. setup_role(Domain, Tail);
  99. {error, conflict} ->
  100. uce_role:update(Domain, #uce_role{id=Name, acl=ACL}),
  101. setup_role(Domain, Tail);
  102. {error, Reason} ->
  103. throw({error, Reason})
  104. end.
  105. setup_root_role(Domain) ->
  106. case catch uce_role:add(Domain, #uce_role{id="root",
  107. acl=[#uce_access{action="all", object="all"}]}) of
  108. {ok, created} ->
  109. ok;
  110. {error, conflict} ->
  111. ok;
  112. {error, _} = Error ->throw(Error)
  113. end.
  114. setup_root_user(Domain, #uce_user{} = User) ->
  115. case catch uce_user:add(Domain, User) of
  116. {ok, UId} ->
  117. uce_user:add_role(Domain, UId, {"root", []});
  118. {error, conflict} ->
  119. ok;
  120. {error, _} = Error -> throw(Error)
  121. end.
  122. setup_bricks(Domain) ->
  123. lists:foreach(fun({Name, Token}) ->
  124. setup_root_user(Domain, #uce_user{name=Name,
  125. auth="password",
  126. credential=Token})
  127. end,
  128. config:get(Domain, bricks)).
  129. setup_admin(Domain) ->
  130. Admin = config:get(Domain, admin),
  131. Name = proplists:get_value(uid, Admin),
  132. Auth = proplists:get_value(auth, Admin),
  133. Credential = proplists:get_value(credential, Admin),
  134. Metadata = proplists:get_value(metadata, Admin, []),
  135. setup_root_user(Domain, #uce_user{name=Name,
  136. auth=Auth,
  137. credential=Credential,
  138. metadata={struct, Metadata}}).
  139. setup_server(Vhost) ->
  140. GC = yaws:create_gconf([{flags, [{auth_log, false},
  141. {copy_errlog, false},
  142. {pick_first_virthost_on_nomatch, false},
  143. {debug, false}
  144. ]},
  145. {logdir, config:get(log_dir)},
  146. {cache_refresh_secs, config:get(cache_refresh)}], "default"),
  147. yaws_api:setconf(GC, []),
  148. yaws:add_server(config:get(Vhost, wwwroot),
  149. [{servername, Vhost},
  150. {listen, config:get(bind_ip)},
  151. {port, config:get(port)},
  152. {opaque, Vhost},
  153. {access_log, true},
  154. {partial_post_size, nolimit},
  155. {appmods, [{"/api/" ++ ?VERSION, uce_appmod}]}]).