2015-08-10 19:44:06 +08:00
-- Creates a new group
2015-08-10 17:07:41 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_CreateGroup ( group_name text )
2015-08-10 19:44:06 +08:00
RETURNS VOID AS $ $
2015-08-13 21:20:20 +08:00
DECLARE
cdb_group_role TEXT ;
2015-08-10 17:07:41 +08:00
BEGIN
2015-08-13 21:20:20 +08:00
EXECUTE ' CREATE ROLE " ' | | cdb_group_role | | ' " NOLOGIN; ' ;
2015-08-10 17:07:41 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 17:07:41 +08:00
2015-08-10 19:40:59 +08:00
-- Drops group and everything that role owns
2015-08-12 01:54:27 +08:00
-- TODO: LIMITATION: in order to drop a role all its owned objects must be dropped before.
-- Right now this is done with DROP OWNED, which can only be done by a superadmin.
-- Not even the role creator can drop the role and the objects it owns.
-- All group owned objects by the group are permissions.
2015-08-10 17:07:41 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_DropGroup ( group_name text )
RETURNS VOID AS $ $
2015-08-12 01:54:27 +08:00
DECLARE
cdb_group_role TEXT ;
2015-08-10 17:07:41 +08:00
BEGIN
2015-08-12 01:54:27 +08:00
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
EXECUTE ' DROP OWNED BY " ' | | cdb_group_role | | ' " ' ;
EXECUTE ' DROP ROLE IF EXISTS " ' | | cdb_group_role | | ' " ' ;
2015-08-10 17:07:41 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 17:21:57 +08:00
2015-08-10 19:46:35 +08:00
-- Renames a group
2015-08-10 17:21:57 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_RenameGroup ( old_group_name text , new_group_name text )
RETURNS VOID AS $ $
BEGIN
2015-08-11 20:08:55 +08:00
EXECUTE ' ALTER ROLE " ' | | cartodb . _CDB_Group_GroupRole ( old_group_name ) | | ' " RENAME TO " ' | | cartodb . _CDB_Group_GroupRole ( new_group_name ) | | ' " ' ;
2015-08-10 17:21:57 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 17:21:57 +08:00
2015-08-10 19:46:35 +08:00
-- Adds a user to a group
2015-08-10 19:40:59 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_AddMember ( group_name text , username text )
RETURNS VOID AS $ $
DECLARE
2015-08-11 20:08:55 +08:00
cdb_group_role TEXT ;
cdb_user_role TEXT ;
2015-08-10 19:40:59 +08:00
BEGIN
2015-08-11 20:08:55 +08:00
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
cdb_user_role : = cartodb . _CDB_User_RoleFromUsername ( username ) ;
EXECUTE ' GRANT " ' | | cdb_group_role | | ' " TO " ' | | cdb_user_role | | ' " ' ;
2015-08-10 19:40:59 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 19:40:59 +08:00
2015-08-10 19:46:35 +08:00
-- Removes a user from a group
2015-08-10 19:40:59 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_RemoveMember ( group_name text , username text )
RETURNS VOID AS $ $
DECLARE
2015-08-11 20:08:55 +08:00
cdb_group_role TEXT ;
cdb_user_role TEXT ;
2015-08-10 19:40:59 +08:00
BEGIN
2015-08-11 20:08:55 +08:00
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
cdb_user_role : = cartodb . _CDB_User_RoleFromUsername ( username ) ;
EXECUTE ' REVOKE " ' | | cdb_group_role | | ' " FROM " ' | | cdb_user_role | | ' " ' ;
2015-08-10 19:40:59 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 19:40:59 +08:00
2015-08-10 19:46:35 +08:00
-- Grants table read permission to a group
2015-08-10 19:40:59 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_Table_GrantRead ( group_name text , username text , table_name text )
RETURNS VOID AS $ $
DECLARE
cdb_group_role TEXT ;
BEGIN
2015-08-10 19:42:27 +08:00
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
2015-08-10 19:40:59 +08:00
EXECUTE ' GRANT USAGE ON SCHEMA " ' | | username | | ' " TO " ' | | cdb_group_role | | ' " ' ;
EXECUTE ' GRANT SELECT ON TABLE " ' | | username | | ' "." ' | | table_name | | ' " TO " ' | | cdb_group_role | | ' " ' ;
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 19:40:59 +08:00
2015-08-10 19:53:57 +08:00
-- Grants table write permission to a group
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_Table_GrantReadWrite ( group_name text , username text , table_name text )
RETURNS VOID AS $ $
DECLARE
cdb_group_role TEXT ;
BEGIN
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
EXECUTE ' GRANT USAGE ON SCHEMA " ' | | username | | ' " TO " ' | | cdb_group_role | | ' " ' ;
EXECUTE ' GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE " ' | | username | | ' "." ' | | table_name | | ' " TO " ' | | cdb_group_role | | ' " ' ;
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 19:53:57 +08:00
2015-08-10 19:46:35 +08:00
-- Revokes all permissions on a table from a group
2015-08-10 19:40:59 +08:00
CREATE OR REPLACE
FUNCTION cartodb . CDB_Group_Table_RevokeAll ( group_name text , username text , table_name text )
RETURNS VOID AS $ $
DECLARE
cdb_group_role TEXT ;
BEGIN
2015-08-10 19:42:27 +08:00
cdb_group_role : = cartodb . _CDB_Group_GroupRole ( group_name ) ;
2015-08-10 19:40:59 +08:00
EXECUTE ' REVOKE ALL ON TABLE " ' | | username | | ' "." ' | | table_name | | ' " FROM " ' | | cdb_group_role | | ' " ' ;
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL VOLATILE ;
2015-08-10 19:40:59 +08:00
2015-08-10 17:21:57 +08:00
- - - - - - - - - - - - - - - - - - - - - --
-- Private functions
- - - - - - - - - - - - - - - - - - - - - --
2015-08-11 20:49:12 +08:00
-- Given a group name returns a role. group_name must be a valid PostgreSQL idenfifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
2015-08-10 17:21:57 +08:00
CREATE OR REPLACE
2015-08-10 19:42:27 +08:00
FUNCTION cartodb . _CDB_Group_GroupRole ( group_name text )
2015-08-10 17:21:57 +08:00
RETURNS TEXT AS $ $
2015-08-10 22:15:04 +08:00
DECLARE
group_role TEXT ;
2015-08-13 02:01:07 +08:00
max_length constant INTEGER : = 60 ;
2015-08-10 17:21:57 +08:00
BEGIN
2015-08-11 20:49:12 +08:00
IF group_name ! ~ ' ^[a-zA-Z_][a-zA-Z0-9_]*$ '
THEN
RAISE EXCEPTION ' Group name (%) must be a valid identifier. See http://www.postgresql.org/docs/9.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS ' , group_name ;
END IF ;
2015-08-13 02:01:07 +08:00
group_role : = current_database ( ) | | ' _g_ ' | | group_name ;
IF LENGTH ( group_role ) > max_length
THEN
RAISE EXCEPTION ' Group name should be shorter. Resulting role must have less than % characters, but it is longer: % ' , max_length , group_role ;
END IF ;
RETURN group_role ;
2015-08-10 17:21:57 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL IMMUTABLE ;
2015-08-10 19:40:59 +08:00
-- Returns the first owner of the schema matching username. Organization user schemas must have one only owner.
CREATE OR REPLACE
2015-08-10 19:42:27 +08:00
FUNCTION cartodb . _CDB_User_RoleFromUsername ( username text )
2015-08-10 19:40:59 +08:00
RETURNS TEXT AS $ $
DECLARE
2015-08-11 20:08:55 +08:00
user_role TEXT ;
2015-08-10 19:40:59 +08:00
BEGIN
2015-08-12 01:54:27 +08:00
-- This was preferred, but non-superadmins won't get results
- - EXECUTE ' SELECT SCHEMA_OWNER FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = $1 LIMIT 1 ' INTO user_role USING username ;
EXECUTE ' SELECT pg_get_userbyid(nspowner) FROM pg_namespace WHERE nspname = $1; ' INTO user_role USING username ;
2015-08-11 20:08:55 +08:00
RETURN user_role ;
2015-08-10 19:40:59 +08:00
END
2015-08-13 21:20:20 +08:00
$ $ LANGUAGE PLPGSQL IMMUTABLE ;