CREATE OR REPLACE FUNCTION git."AccessType" (
        gituserid integer,
        gitrepo character varying
)
RETURNS git.gitrepoaccesstype
LANGUAGE plpgsql
AS $code$
DECLARE
        selectedrepo git.gitrepoaccesstype;
BEGIN
	
		/* user.repo */
        SELECT MAX("accessType")
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON "repoID"=repos."ID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
		RETURN selectedrepo;
		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		
		/* groupe.repo */
        SELECT MAX('accessType')
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON repoID=repos."ID"
		JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        WHERE repos.path=gitrepo AND appartenances."utilisateurID"=gituserid;
		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		
		/* user.repoGroup */
        SELECT MAX('accessType')
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		
		/* groupe.repoGroupe */
        SELECT MAX('accessType')
        INTO selectedrepo
        FROM git.perms
		JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
        RETURN selectedRepo;
		
END
$code$
