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