 
 
 --
--- Name: sec_event; Type: TABLE; Schema: public; Owner: postgres; Tablespace: 
---
-
-CREATE TABLE sec_event (
+-- Name: sec_event_log; Type: TABLE; Schema: public; Owner: postgres; Tablespace: 
+--
+
+CREATE TABLE sec_event_log (
     whenfired timestamp with time zone NOT NULL,
     event_kind integer NOT NULL,
     event_status integer NOT NULL,
     session_id integer NOT NULL,
-    context xml
+    context xml,
+    user_name text,
+    auth_path_id integer,
+    tokenvalue text
 );
 
 
-ALTER TABLE public.sec_event OWNER TO postgres;
-
---
--- Name: COLUMN sec_event.whenfired; Type: COMMENT; Schema: public; Owner: postgres
---
-
-COMMENT ON COLUMN sec_event.whenfired IS 'дата/вермя начала события';
-
-
---
--- Name: COLUMN sec_event.event_kind; Type: COMMENT; Schema: public; Owner: postgres
---
-
-COMMENT ON COLUMN sec_event.event_kind IS 'тип события';
-
-
---
--- Name: COLUMN sec_event.event_status; Type: COMMENT; Schema: public; Owner: postgres
---
-
-COMMENT ON COLUMN sec_event.event_status IS 'статус события';
-
-
---
--- Name: COLUMN sec_event.session_id; Type: COMMENT; Schema: public; Owner: postgres
---
-
-COMMENT ON COLUMN sec_event.session_id IS 'идентификатор сессии';
-
-
---
--- Name: COLUMN sec_event.context; Type: COMMENT; Schema: public; Owner: postgres
---
-
-COMMENT ON COLUMN sec_event.context IS 'контекст (аттрибуты, параметры) события';
-
-
---
--- Name: sec_event_start(integer, text, text, xml); Type: FUNCTION; Schema: public; Owner: postgres
---
-
-CREATE FUNCTION sec_event_start(p_session_id integer, p_class_name text, p_event_name text, p_context xml DEFAULT NULL::xml) RETURNS sec_event
+ALTER TABLE public.sec_event_log OWNER TO postgres;
+
+--
+-- Name: COLUMN sec_event_log.whenfired; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.whenfired IS 'дата/вермя начала события';
+
+
+--
+-- Name: COLUMN sec_event_log.event_kind; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.event_kind IS 'тип события';
+
+
+--
+-- Name: COLUMN sec_event_log.event_status; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.event_status IS 'статус события';
+
+
+--
+-- Name: COLUMN sec_event_log.session_id; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.session_id IS 'идентификатор сессии';
+
+
+--
+-- Name: COLUMN sec_event_log.context; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.context IS 'контекст (аттрибуты, параметры) события';
+
+
+--
+-- Name: COLUMN sec_event_log.user_name; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.user_name IS 'имя учетной записи';
+
+
+--
+-- Name: COLUMN sec_event_log.auth_path_id; Type: COMMENT; Schema: public; Owner: postgres
+--
+
+COMMENT ON COLUMN sec_event_log.auth_path_id IS 'источник аутентификации';
+
+
+--
+-- Name: sec_event_log_start(integer, text, text, xml, text, integer, text); Type: FUNCTION; Schema: public; Owner: postgres
+--
+
+CREATE FUNCTION sec_event_log_start(p_session_id integer, p_class_name text, p_event_name text, p_context xml DEFAULT NULL::xml, p_user_name text DEFAULT NULL::text, p_auth_path_id integer DEFAULT NULL::integer, p_tokenvalue text DEFAULT NULL::text) RETURNS sec_event_log
     LANGUAGE plpgsql COST 3
     AS $$
 declare
   -- регистрирует начало события с указанным именем и именем класса
-  res  sec_event;
+  res  sec_event_log;
 begin
 --  BEGIN SUBTRANSACTION;
-  insert into sec_event (session_id, whenfired, event_kind, event_status, context)
-  values (p_session_id, clock_timestamp(), (env_event_kind(p_class_name, p_event_name)).id, (env_event_status())."STARTED", p_context)
+  insert into sec_event_log (session_id, whenfired, event_kind, event_status, context, user_name, auth_path_id, tokenvalue)
+  values (p_session_id, clock_timestamp(), (lml_event(p_class_name, p_event_name)).id, (lml_event_status())."STARTED", p_context, p_user_name, p_auth_path_id, p_tokenvalue)
   returning * into res;
 --  COMMIT SUBTRANSACTION;
   return res;

begin
  -- текущая сессия
  select * into l_session from sec_session_valid(p_session_id);
  -- проверка наличия полномочий
  ok := sec_session_has_permission(p_session_id, 'sec_user_authcred.set_valid_period');
  if (not ok) then -- полномочий нет
    -- 'SEC00004', 'access denied. has no permission (%s)'
    raise exception insufficient_privilege using message = env_resource_text_format('SEC00004', 'sec_user_authcred.set_valid_period');
  end if;
  -- полномочий достаточно
  l_user := sec_user_by_id(p_user_id);
  l_uac := sec_user_authcred_by_pk(l_user.user_id, p_auth_path_id);
  -- сохраняем текщие учетные данные в журнал изменений
  insert into sec_user_authcred_log (log_id, when_logged, user_id, auth_path_id, credential, credential_hash, valid_from, valid_till)
  values (default, clock_timestamp(), l_uac.user_id, l_uac.auth_path_id, l_uac.credential, l_uac.credential_hash, l_uac.valid_from, l_uac.valid_till);
  -- ограничиваем срок действия указанных учетных данных
  update sec_user_authcred acr
     set valid_from = coalesce(p_valid_from, valid_from, now),
         valid_till = coalesce(p_valid_till, valid_till, now)
   where user_id = l_user.user_id
     and auth_path_id = p_auth_path_id
  returning * into l_uac;
  return l_uac;

25	2016-01-06 02:24:43.068885+03	\N	40	2016-01-06 02:24:43.069305+03
1	2016-01-06 02:30:12.134391+03	\N	41	2016-01-06 02:30:12.134687+03
25	2016-01-06 02:30:12.150434+03	\N	42	2016-01-06 02:30:12.150643+03
1	2016-01-06 23:48:35.364139+03	\N	94	2016-01-06 23:48:35.364353+03
25	2016-01-06 23:48:35.40549+03	\N	96	2016-01-06 23:48:35.405787+03
1	2016-01-07 00:23:49.704738+03	\N	103	2016-01-07 00:23:49.705009+03
25	2016-01-07 00:23:49.718982+03	\N	104	2016-01-07 00:23:49.71919+03
1	2016-01-07 00:23:59.738338+03	\N	105	2016-01-07 00:23:59.738695+03
25	2016-01-07 00:23:59.75326+03	\N	106	2016-01-07 00:23:59.753469+03
1	2016-01-07 00:25:13.28023+03	\N	107	2016-01-07 00:25:13.280443+03
25	2016-01-07 00:25:13.295131+03	\N	108	2016-01-07 00:25:13.295342+03
1	2016-01-07 00:25:13.920181+03	\N	109	2016-01-07 00:25:13.920487+03
25	2016-01-07 00:25:13.934574+03	\N	110	2016-01-07 00:25:13.934878+03
1	2016-01-07 00:28:58.357206+03	\N	111	2016-01-07 00:28:58.357484+03
25	2016-01-07 00:28:58.373675+03	\N	112	2016-01-07 00:28:58.374031+03
1	2016-01-07 01:52:23.623494+03	\N	113	2016-01-07 01:52:23.625809+03
25	2016-01-07 01:52:23.664417+03	\N	114	2016-01-07 01:52:23.66488+03
25	2016-01-07 01:56:08.150061+03	\N	115	2016-01-07 01:56:08.151782+03
25	2016-01-07 01:58:11.868894+03	\N	119	2016-01-07 01:58:11.869415+03
25	2016-01-07 01:59:24.262703+03	\N	120	2016-01-07 01:59:24.263229+03
1	2016-01-07 02:18:15.185377+03	\N	122	2016-01-07 02:18:15.186075+03
25	2016-01-07 02:18:15.205314+03	\N	123	2016-01-07 02:18:15.205836+03
25	2016-01-07 02:37:13.170203+03	2016-01-07 02:37:13.246044+03	125	2016-01-07 02:37:13.246044+03
1	2016-01-07 02:37:13.13398+03	2016-01-07 02:37:13.250909+03	124	2016-01-07 02:37:13.250909+03
\.


