remove dropIfExists call from functions/views as to not drop them unexpectedly
[xtuple] / foundation-database / public / functions / usercanlogin.sql
1 --DROP VIEW     IF EXISTS usr;
2 --DROP FUNCTION IF EXISTS userCanLogin(TEXT);
3 CREATE OR REPLACE FUNCTION userCanLogin(pUsername TEXT) RETURNS BOOLEAN AS $$
4 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
5 -- See www.xtuple.com/CPAL for the full text of the software license.
6 DECLARE
7   _isactive  BOOLEAN;
8   _mode      TEXT;
9 BEGIN
10   IF (isDBA(pUsername) OR userCanCreateUsers(pUsername)) THEN
11     RETURN TRUE;
12
13   ELSIF (pg_has_role(pUsername, 'xtrole', 'member')) THEN
14     _mode := COALESCE(fetchMetricText('AllowedUserLogins'), '');
15
16     IF (_mode = 'AdminOnly') THEN
17       RETURN FALSE; -- administrators were checked above
18     END IF;
19
20     IF (_mode NOT IN ('AdminOnly','ActiveOnly','Any')) THEN
21       _mode := 'ActiveOnly';
22     END IF;
23
24     SELECT (usrpref_value = 't') INTO _isactive
25       FROM usrpref
26      WHERE usrpref_username = pUsername
27        AND usrpref_name = 'active';
28
29     IF (_isactive OR _mode = 'Any') THEN
30       RETURN TRUE;
31     END IF;
32   END IF;
33
34   RETURN FALSE;
35 END;
36 $$ LANGUAGE 'plpgsql';
37