numerus/deploy/login.sql
jordi fita mas b63f7e7c54 Change find_user_role’s and login’s volatility to stable
According to PostgreSQL’s manual[0]:

  “STABLE indicates that the function cannot modify the database, and
   that within a single table scan it will consistently return the same
   result for the same argument values, but that its result could change
   across SQL statements.”

This definition matches both functions.  Moreover, find_user_role did
not need to be written in plpgsql, that i assume—but did not test—are
slower than sql functions.

[0]: https://www.postgresql.org/docs/14/sql-createfunction.html
2023-01-16 10:02:31 +01:00

34 lines
713 B
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Deploy numerus:login to pg
-- requires: roles
-- requires: schema_numerus
-- requires: email
-- requires: user
-- requires: find_user_role
begin;
set search_path to numerus, auth;
create or replace function login(email email, password text) returns name as
$$
declare
role name;
begin
select auth.find_user_role(email, password) into role;
if role is null then
raise invalid_password using message = 'invalid user or password';
end if;
return role;
end;
$$
language plpgsql
stable
security definer;
comment on function login(email, text) is
'Checks that the email and password pair is valid and returns the users databasse role.';
grant execute on function login(email, text) to guest;
commit;