--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: 
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


SET search_path = public, pg_catalog;

--
-- 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;
	    IF val is null then
	        val := FALSE;
	    end if;
	end if;
    END IF;
END;
$$;


ALTER FUNCTION public.sp_lu_get_user_permission(_user_id integer, _permisson_name character varying, OUT val boolean) OWNER TO dev;

--
-- 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;


ALTER TABLE public.luticate_groups_id_seq OWNER TO dev;

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
);


ALTER TABLE public.luticate_groups OWNER TO dev;

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

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


ALTER TABLE public.luticate_permissions OWNER TO dev;

--
-- 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
);


ALTER TABLE public.luticate_permissions_groups OWNER TO dev;

--
-- 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
);


ALTER TABLE public.luticate_permissions_users OWNER TO dev;

--
-- 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;


ALTER TABLE public.luticate_users_id_seq OWNER TO dev;

--
-- 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
);


ALTER TABLE public.luticate_users OWNER TO dev;

--
-- 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
);


ALTER TABLE public.luticate_users_groups OWNER TO dev;

--
-- 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_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_users_groups
    ADD CONSTRAINT foreign_group FOREIGN KEY (group_id) REFERENCES luticate_groups(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);


--
-- 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);


--
-- 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);


--
-- 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);


--
-- 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);


--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

