2012-02-06 00:09:21 +01:00
|
|
|
--
|
|
|
|
-- Create a new user
|
|
|
|
--
|
|
|
|
|
2012-02-06 08:36:01 +01:00
|
|
|
CREATE OR REPLACE FUNCTION users_add( _email TEXT , _salt TEXT , _iters INT , _hash TEXT , _name TEXT )
|
2012-02-06 00:09:21 +01:00
|
|
|
RETURNS INT
|
|
|
|
LANGUAGE PLPGSQL
|
|
|
|
STRICT VOLATILE SECURITY INVOKER
|
|
|
|
AS $users_add$
|
|
|
|
BEGIN
|
2012-02-06 08:36:01 +01:00
|
|
|
IF _name = '' THEN
|
|
|
|
_name := NULL;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
INSERT INTO users ( user_email , user_salt , user_iterations , user_hash , user_display_name )
|
|
|
|
VALUES ( _email , _salt , _iters , _hash , _name );
|
2012-02-06 00:09:21 +01:00
|
|
|
RETURN 0;
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
END;
|
|
|
|
$users_add$;
|
|
|
|
|
2012-02-06 08:36:01 +01:00
|
|
|
REVOKE EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT , TEXT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT , TEXT) TO :webapp_user;
|
2012-02-06 08:55:33 +01:00
|
|
|
|
|
|
|
|
2012-02-06 10:03:11 +01:00
|
|
|
--
|
|
|
|
-- Update an user's address and display name
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION users_edit( _id INT , _email TEXT , _name TEXT )
|
|
|
|
RETURNS INT
|
|
|
|
LANGUAGE PLPGSQL
|
|
|
|
STRICT VOLATILE SECURITY INVOKER
|
|
|
|
AS $users_edit$
|
|
|
|
BEGIN
|
|
|
|
IF _name = '' THEN
|
|
|
|
_name := NULL;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
UPDATE users SET user_email = _email , user_display_name = _name
|
|
|
|
WHERE user_id = _id;
|
|
|
|
RETURN ( CASE WHEN FOUND THEN 0 ELSE 2 END );
|
|
|
|
EXCEPTION
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RETURN 1;
|
|
|
|
END;
|
|
|
|
$users_edit$;
|
|
|
|
|
|
|
|
REVOKE EXECUTE ON FUNCTION users_edit( INT , TEXT , TEXT ) FROM PUBLIC;
|
|
|
|
GRANT EXECUTE ON FUNCTION users_edit( INT , TEXT , TEXT ) TO :webapp_user;
|
|
|
|
|
2012-02-06 08:55:33 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- View that lists users and adds the string to use when displaying
|
|
|
|
--
|
|
|
|
|
|
|
|
DROP VIEW IF EXISTS users_view;
|
|
|
|
CREATE VIEW users_view
|
|
|
|
AS SELECT * , ( CASE
|
|
|
|
WHEN user_display_name IS NULL THEN
|
|
|
|
user_email
|
|
|
|
ELSE
|
|
|
|
user_display_name
|
|
|
|
END ) AS user_view_name
|
|
|
|
FROM users;
|
|
|
|
|
|
|
|
GRANT SELECT ON users_view TO :webapp_user;
|