Skip to content

Commit

Permalink
Merge pull request #18 from fycth/master
Browse files Browse the repository at this point in the history
moving to kvs 3.1
  • Loading branch information
5HT committed Jan 30, 2016
2 parents 0df82b5 + d8fa35e commit 40e8082
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 30 deletions.
26 changes: 26 additions & 0 deletions include/avz_user.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-ifndef(AVZ_USER_HRL).
-define(AVZ_USER_HRL, true).

-include_lib("kvs/include/kvs.hrl").

-record(avz_user,
{?ITERATOR(feed, true),
email,
username,
password,
display_name,
register_date,
tokens = [],
images,
names,
surnames,
birth,
sex,
date,
status,
zone,
type,
comments = []
}).

-endif.
6 changes: 3 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{lib_dirs, ["deps",".."]}.
{deps_dir, ["deps"]}.
{deps, [
{nitro, ".*", {git, "git://github.com/synrc/nitro", {tag,"master"}}},
{n2o, ".*", {git, "git://github.com/synrc/n2o", {tag,"master"}}},
{kvs, ".*", {git, "git://github.com/synrc/kvs", {tag,"2.8"}}},
{nitro, ".*", {git, "git://github.com/synrc/nitro", {tag,"1.1"}}},
{n2o, ".*", {git, "git://github.com/synrc/n2o", {tag,"3.1"}}},
{kvs, ".*", {git, "git://github.com/synrc/kvs", {tag,"3.1"}}},
{erlydtl, ".*", {git, "git://github.com/evanmiller/erlydtl.git", {tag,"0.8.0"}}}
]}.
{erlydtl_opts, [
Expand Down
10 changes: 5 additions & 5 deletions src/avz.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-module(avz).
-author('Maxim Sokhatsky').
-compile(export_all).
-include_lib("avz/include/avz_user.hrl").
-include_lib("avz/include/avz.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").

sha(Pass) -> crypto:hmac(wf:config(n2o,hmac,sha256),n2o_secret:secret(),wf:to_binary(Pass)).
update({K,V},P) -> wf:setkey(K,1,case P of undefined -> []; _P -> _P end,{K,V}).
Expand All @@ -19,8 +19,8 @@ buttons(Methods) -> [ M:login_button() || M <- Methods].
event(init) -> [];
event(logout) -> wf:user(undefined), wf:redirect(?LOGIN_PAGE);
event(to_login) -> wf:redirect(?LOGIN_PAGE);
event({register, #user{}=U}) -> kvs:add(U), login_user(U); % sample
event({login, #user{}=U, N}) -> Updated = merge(U,N), kvs:put(Updated), login_user(Updated); % sample
event({register, #avz_user{}=U}) -> kvs:put(U), login_user(U); % sample
event({login, #avz_user{}=U, N}) -> Updated = merge(U,N), kvs:put(Updated), login_user(Updated); % sample
event({Method,Event}) -> Method:event({Method,Event});
event(Ev) -> wf:info(?MODULE,"Page Event ~p",[Ev]).

Expand All @@ -33,12 +33,12 @@ login_user(User) -> wf:user(User), wf:redirect(?AFTER_LOGIN).
login(_Key, [{error, E}|_Rest])-> wf:info(?MODULE,"Auth Error: ~p", [E]);
login(Key, Args) ->
n2o_session:ensure_sid([],?CTX,[]),
case kvs:get(user,Key:email_prop(Args,Key)) of
case kvs:get(avz_user,Key:email_prop(Args,Key)) of
{ok,Existed} ->
RegData = Key:registration_data(Args, Key, Existed),
(?CTX#cx.module):event({login, Existed, RegData});
{error,_} ->
RegData = Key:registration_data(Args, Key, #user{}),
RegData = Key:registration_data(Args, Key, #avz_user{}),
(?CTX#cx.module):event({register, RegData});
U -> wf:info(?MODULE,"Unknown Login: ~p",[U]) end.

Expand Down
11 changes: 11 additions & 0 deletions src/avz_user.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

-module(avz_user).
-include("avz_user.hrl").
-include_lib("kvs/include/metainfo.hrl").
-compile(export_all).
metainfo() ->
#schema{name = kvs,
tables = [#table{name = avz_user,
container = feed,
fields = record_info(fields, avz_user),
keys = [email]}]}.
6 changes: 3 additions & 3 deletions src/email.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
-include_lib("avz/include/avz.hrl").
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").
-compile(export_all).
-export(?API).

registration_data(Props, email, Ori)->
Email = email_prop(Props, email),
Ori#user{ id = Email,
Ori#avz_user{ id = Email,
display_name = Email,
email = Email,
register_date = os:timestamp(),
tokens = avz:update({email,Email},Ori#user.tokens),
tokens = avz:update({email,Email},Ori#avz_user.tokens),
status = ok,
password = avz:sha(proplists:get_value(<<"password">>,Props))}.

Expand Down
8 changes: 4 additions & 4 deletions src/facebook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("avz/include/avz.hrl").
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").

-compile(export_all).
-export(?API).
Expand All @@ -23,13 +23,13 @@ registration_data(Props, facebook, Ori)->
Email = email_prop(Props, facebook),
[UserName|_] = string:tokens(binary_to_list(Email),"@"),
Cover = case proplists:get_value(<<"cover">>,Props) of undefined -> ""; {P} -> case proplists:get_value(<<"source">>,P) of undefined -> ""; C -> binary_to_list(C) end end,
Ori#user{ id = Email,
Ori#avz_user{ id = Email,
display_name = UserName,
images = avz:update({fb_cover,Cover},avz:update({fb_avatar,"https://graph.facebook.com/" ++ binary_to_list(Id) ++ "/picture?type=large"},Ori#user.images)),
images = avz:update({fb_cover,Cover},avz:update({fb_avatar,"https://graph.facebook.com/" ++ binary_to_list(Id) ++ "/picture?type=large"},Ori#avz_user.images)),
email = Email,
names = proplists:get_value(<<"first_name">>, Props),
surnames = proplists:get_value(<<"last_name">>, Props),
tokens = avz:update({facebook,Id},Ori#user.tokens),
tokens = avz:update({facebook,Id},Ori#avz_user.tokens),
birth = {element(3, BirthDay), element(1, BirthDay), element(2, BirthDay)},
register_date = os:timestamp(),
status = ok }.
Expand Down
8 changes: 4 additions & 4 deletions src/github.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-author('Andrii Zadorozhnii').
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").
-include_lib("avz/include/avz.hrl").
-compile(export_all).
-export(?API).
Expand Down Expand Up @@ -52,14 +52,14 @@ registration_data(Props, github, Ori) ->
Id = proplists:get_value(<<"id">>, Props),
Name = proplists:get_value(<<"name">>, Props),
Email = email_prop(Props, github),
Ori#user{ id= Email,
Ori#avz_user{ id= Email,
username = binary_to_list(proplists:get_value(<<"login">>, Props)),
display_name = Name,
images = avz:update({gh_avatar,proplists:get_value(<<"avatar_url">>, Props)},Ori#user.images),
images = avz:update({gh_avatar,proplists:get_value(<<"avatar_url">>, Props)},Ori#avz_user.images),
email = Email,
names = Name,
surnames = [],
tokens = avz:update({github,Id},Ori#user.tokens),
tokens = avz:update({github,Id},Ori#avz_user.tokens),
register_date = os:timestamp(),
status = ok }.

Expand Down
8 changes: 4 additions & 4 deletions src/google.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("avz/include/avz.hrl").
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").
-compile(export_all).
-export(?API).
-define(GPLUS_CLIENT_ID, application:get_env(web, gplus_client_id, [])).
Expand All @@ -18,13 +18,13 @@ registration_data(Props, google, Ori)->
GivenName = proplists:get_value(<<"given_name">>, Props),
FamilyName = proplists:get_value(<<"family_name">>, Props),
Email = email_prop(Props,google),
Ori#user{ id = Email,
Ori#avz_user{ id = Email,
display_name = proplists:get_value(<<"displayName">>, Props),
images = avz:update({google_avatar,Image},Ori#user.images),
images = avz:update({google_avatar,Image},Ori#avz_user.images),
email = Email,
names = GivenName,
surnames = FamilyName,
tokens = avz:update({google,Id},Ori#user.tokens),
tokens = avz:update({google,Id},Ori#avz_user.tokens),
register_date = os:timestamp(),
sex = proplists:get_value(<<"gender">>, Props),
status = ok }.
Expand Down
6 changes: 3 additions & 3 deletions src/microsoft.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("avz/include/avz.hrl").
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").
-compile(export_all).
-export(?API).
-define(CLIENT_ID, "000000004C0FEEB0").
Expand All @@ -24,12 +24,12 @@ registration_data(Props, microsoft, Ori)->
GivenName = proplists:get_value(<<"first_name">>, Props),
FamilyName = proplists:get_value(<<"last_name">>, Props),
Email = email_prop(Props,microsoft),
Ori#user{ id = Email,
Ori#avz_user{ id = Email,
display_name = proplists:get_value(<<"name">>, Props),
email = Email,
names = GivenName,
surnames = FamilyName,
tokens = avz:update({microsoft,Id},Ori#user.tokens),
tokens = avz:update({microsoft,Id},Ori#avz_user.tokens),
register_date = os:timestamp(),
sex = proplists:get_value(<<"gender">>, Props),
status = ok }.
Expand Down
8 changes: 4 additions & 4 deletions src/twitter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/wf.hrl").
-include_lib("avz/include/avz.hrl").
-include_lib("kvs/include/user.hrl").
-include_lib("avz/include/avz_user.hrl").
-compile(export_all).
-export(?API).
-define(CONSUMER_KEY, application:get_env(web, tw_consumer_key, [])).
Expand All @@ -14,14 +14,14 @@ registration_data(Props, twitter, Ori)->
Id = proplists:get_value(<<"id_str">>, Props),
UserName = binary_to_list(proplists:get_value(<<"screen_name">>, Props)),
Email = email_prop(Props,twitter),
Ori#user{ id = Email,
Ori#avz_user{ id = Email,
username = re:replace(UserName, "\\.", "_", [{return, list}]),
display_name = proplists:get_value(<<"screen_name">>, Props),
images = avz:update({tw_avatar,proplists:get_value(<<"profile_image_url">>, Props)},Ori#user.images),
images = avz:update({tw_avatar,proplists:get_value(<<"profile_image_url">>, Props)},Ori#avz_user.images),
names = proplists:get_value(<<"name">>, Props),
email = Email,
surnames = [],
tokens = avz:update({twitter,Id},Ori#user.tokens),
tokens = avz:update({twitter,Id},Ori#avz_user.tokens),
register_date = os:timestamp(),
status = ok }.

Expand Down

0 comments on commit 40e8082

Please sign in to comment.