summaryrefslogtreecommitdiff
path: root/sql/functions/auth.insert_user.sql
blob: 597fd399e3c9e5e9a5569213becf0afb4f129dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
drop function auth.insert_user(
    p_username character varying,
    p_pwd character varying,
    p_first_name character varying,
    p_last_name character varying,
    p_email character varying,
    p_phone character varying,
    p_active boolean
);

create or replace function auth.insert_user (
    p_username character varying,
    p_pwd character varying,
    p_first_name character varying,
    p_last_name character varying,
    p_email character varying,
    p_phone character varying,
    p_active boolean
)
returns bigint
as $$
declare
  l_id bigint;
begin
    select u.id into l_id
    from auth.users u
    where u.username = p_username
        and u.pwd = digest(p_pwd, 'sha512');

    if(l_id is null) then
        insert into auth.users (
            username,
            pwd,
            first_name,
            last_name,
            email,
            phone,
            active
        ) values (
            p_username,
            digest(p_pwd, 'sha512'),
            p_first_name,
            p_last_name,
            p_email,
            p_phone,
            p_active
        );

        select u.id into l_id
        from auth.users u
        where u.username = p_username
            and u.pwd = digest(p_pwd, 'sha512');

        return l_id;
    else
        return null;
    end if;
end;
$$ language plpgsql;