--
-- PostgreSQL database dump
--
--
-- Name: sp_lu_get_all_user_permission(integer); Type: FUNCTION; Schema: public; Owner: dev
--

CREATE FUNCTION sp_lu_get_all_user_permission(_user_id integer, OUT _permisson_name character varying, OUT val boolean) RETURNS SETOF record
LANGUAGE sql IMMUTABLE SECURITY DEFINER COST 10
AS $$
	SELECT p.name, sp_lu_get_user_permission(_user_id, p.name) FROM luticate_permissions p
$$;

--
-- Name: sp_lu_get_user_permission(integer, character varying); Type: FUNCTION; Schema: public; Owner: dev
--

CREATE FUNCTION sp_lu_get_user_permission(_user_id integer, _permisson_name character varying, OUT val boolean) RETURNS boolean
LANGUAGE plpgsql IMMUTABLE SECURITY DEFINER COST 10
AS $$
DECLARE
groups int[];
g INT;
BEGIN
    SELECT "value" INTO val FROM luticate_permissions_users WHERE "user_id" = _user_id AND "name" = _permisson_name;
    IF val is null THEN
	SELECT array_agg("group_id") INTO groups FROM luticate_users_groups  WHERE "user_id" = _user_id;
	IF groups is not null then
	    FOREACH g in ARRAY groups LOOP
	        SELECT "value" INTO val FROM luticate_permissions_groups WHERE "group_id" = g AND "name" = _permisson_name;
	        IF val = FALSE THEN
	            EXIT;
	        END IF;
	    END LOOP;
	END if;
	IF val is null then
	    SELECT "value" INTO val FROM luticate_permissions WHERE "name" = _permisson_name;
	end if;
    END IF;
END;
$$;

--
-- Name: luticate_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: dev
--

CREATE SEQUENCE luticate_groups_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: luticate_groups; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_groups (
    id integer DEFAULT nextval('luticate_groups_id_seq'::regclass) NOT NULL,
    name character varying NOT NULL
);

--
-- Name: luticate_permissions; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_permissions (
    name character varying(128) NOT NULL,
    value boolean NOT NULL
);

--
-- Name: luticate_permissions_groups; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_permissions_groups (
    name character varying(128) NOT NULL,
    value boolean NOT NULL,
    group_id integer NOT NULL
);

--
-- Name: luticate_permissions_users; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_permissions_users (
    name character varying(128) NOT NULL,
    value boolean,
    user_id integer NOT NULL
);

--
-- Name: luticate_users_id_seq; Type: SEQUENCE; Schema: public; Owner: dev
--

CREATE SEQUENCE luticate_users_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

--
-- Name: luticate_users; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_users (
    id integer DEFAULT nextval('luticate_users_id_seq'::regclass) NOT NULL,
    username character varying(255) NOT NULL,
    email character varying(255) NOT NULL,
    password character varying(255) NOT NULL,
    salt character varying(10) NOT NULL,
    profile_id integer,
    firstname character varying(255) NOT NULL,
    lastname character varying(255) NOT NULL
);

--
-- Name: luticate_users_groups; Type: TABLE; Schema: public; Owner: dev; Tablespace: 
--

CREATE TABLE luticate_users_groups (
    user_id integer NOT NULL,
    group_id integer NOT NULL
);

--
-- Name: luticate_groups_name_key; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_groups
ADD CONSTRAINT luticate_groups_name_key UNIQUE (name);


--
-- Name: luticate_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_groups
ADD CONSTRAINT luticate_groups_pkey PRIMARY KEY (id);


--
-- Name: luticate_permissions_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_permissions
ADD CONSTRAINT luticate_permissions_pkey PRIMARY KEY (name);


--
-- Name: luticate_permissions_users_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_permissions_users
ADD CONSTRAINT luticate_permissions_users_pkey PRIMARY KEY (name, user_id);


--
-- Name: luticate_users_email_key; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_users
ADD CONSTRAINT luticate_users_email_key UNIQUE (email);


--
-- Name: luticate_users_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_users_groups
ADD CONSTRAINT luticate_users_groups_pkey PRIMARY KEY (user_id, group_id);


--
-- Name: luticate_users_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_users
ADD CONSTRAINT luticate_users_pkey PRIMARY KEY (id);


--
-- Name: luticate_users_username_key; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_users
ADD CONSTRAINT luticate_users_username_key UNIQUE (username);


--
-- Name: pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace: 
--

ALTER TABLE ONLY luticate_permissions_groups
ADD CONSTRAINT pkey PRIMARY KEY (name, group_id);


--
-- Name: foreign_group; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_permissions_groups
ADD CONSTRAINT foreign_group FOREIGN KEY (group_id) REFERENCES luticate_groups(id) ON DELETE CASCADE;


--
-- Name: foreign_group; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_users_groups
ADD CONSTRAINT foreign_group FOREIGN KEY (group_id) REFERENCES luticate_groups(id) ON DELETE CASCADE;


--
-- Name: foreign_name; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_permissions_groups
ADD CONSTRAINT foreign_name FOREIGN KEY (name) REFERENCES luticate_permissions(name) ON DELETE CASCADE;


--
-- Name: foreign_name; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_permissions_users
ADD CONSTRAINT foreign_name FOREIGN KEY (name) REFERENCES luticate_permissions(name) ON DELETE CASCADE;


--
-- Name: foreign_user; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_permissions_users
ADD CONSTRAINT foreign_user FOREIGN KEY (user_id) REFERENCES luticate_users(id) ON DELETE CASCADE;


--
-- Name: foreign_user; Type: FK CONSTRAINT; Schema: public; Owner: dev
--

ALTER TABLE ONLY luticate_users_groups
ADD CONSTRAINT foreign_user FOREIGN KEY (user_id) REFERENCES luticate_users(id) ON DELETE CASCADE;

--
-- PostgreSQL database dump complete
--
