aboutsummaryrefslogblamecommitdiffstats
path: root/server/cldb.erl
blob: 982aafc02060d3e7eb5ff7273bb77cc97de22c31 (plain) (tree)
1
2
3
4
5
6
7
8
9


                    
                                                                                    
                                             

         
                                   
                   
                                                                         

                                          

                 
                                                           
            
                          





                                 







                                                                                        

















                                               


                                     
                
% The clientdatabase

-module(cldb).
-export([init/0, ask/2, login/2, decVote/2, incVote/2, register/3, check_rights/2]).
-record(user, {name, passwd, votes, rights}).

init() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(user, [{attributes, record_info(fields, user)}]),
    io:format("Userdb up and running \n").
    
ask(User, Pwd) ->
    F = fun() ->
                mnesia:match_object({user, User, Pwd, '_'})
        end,
    mnesia:transaction(F).
    
                
    
login(User, Pwd) ->
    {_, Reason} = ask(User, Pwd),
    Reason.

register(User, Pwd, Root) ->
    F = fun() ->
                mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root})
        end,
    {_, Reason} = mnesia:transaction(F),
    Reason.

decVote(User, Pwd) ->
    F = fun() ->
                [Head|_] = ask(User, Pwd),
                Votes = Head#user.votes - 1,
                New = Head#user{votes = Votes},
                mnesia:write(New)
        end,
    mnesia:transaction(F).

incVote(User, Pwd) ->
    F = fun() ->
                [Head|_] = ask(User, Pwd),
                Votes = Head#user.votes + 1,
                New = Head#user{votes = Votes},
                mnesia:write(New)
        end,
    mnesia:transaction(F).

check_rights(User, Name) ->
    {_, _, Rights} = ask(User, Name),
    Rights.