From 995e73c3b89e3876145a93bdffaa9cc2f1b1ac3c Mon Sep 17 00:00:00 2001
From: Anton Bagliy <taccessviolation@gmail.com>
Date: Thu, 10 Jan 2019 19:55:10 +0300
Subject: [PATCH] ADD: log_sigin_detailed table with openid parameters and
 error messages #347

---
 db/postgresql/openid_logs_10_01_19.sql        | 33 +++++++++++++++++++
 .../classes/Controller/Handler/Sign.php       |  5 ++-
 .../application/classes/Model/Logs.php        | 13 ++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 db/postgresql/openid_logs_10_01_19.sql

diff --git a/db/postgresql/openid_logs_10_01_19.sql b/db/postgresql/openid_logs_10_01_19.sql
new file mode 100644
index 000000000..ff9ba5bbf
--- /dev/null
+++ b/db/postgresql/openid_logs_10_01_19.sql
@@ -0,0 +1,33 @@
+
+CREATE SEQUENCE seq_logs_signin_detailed
+  START WITH 1
+  INCREMENT BY 1
+  NO MINVALUE
+  NO MAXVALUE
+  CACHE 1;
+
+
+CREATE TABLE logs_signin_detailed (
+  id integer DEFAULT nextval('seq_logs_signin_detailed'::regclass) NOT NULL,
+  accountid integer,
+  globalkey character varying NOT NULL,
+  isstudent integer,
+  isstaff integer,
+  error_message character varying,
+  date timestamp(0) without time zone DEFAULT now() NOT NULL
+);
+
+CREATE INDEX key_logs_signin_detailed ON logs_signin_detailed USING btree (globalkey);
+
+ALTER TABLE ONLY logs_signin_detailed
+  ADD CONSTRAINT logs_signin_detailed_pkey PRIMARY KEY (id);
+
+CREATE OR REPLACE FUNCTION public.log_sigindetailed(paccountid integer, pisstaff integer, pisstudent integer, pglobalkey character varying, pmessage character varying)
+  RETURNS integer
+LANGUAGE plpgsql
+AS $function$
+begin
+  INSERT INTO logs_signin_detailed (AccountID, GlobalKey, IsStudent, IsStaff, error_message) VALUES (paccountid, pglobalkey, pisstudent, pisstaff, pmessage);
+  RETURN 0;
+END
+$function$;
diff --git a/~dev_rating/application/classes/Controller/Handler/Sign.php b/~dev_rating/application/classes/Controller/Handler/Sign.php
index 9d948088c..28062fc63 100644
--- a/~dev_rating/application/classes/Controller/Handler/Sign.php
+++ b/~dev_rating/application/classes/Controller/Handler/Sign.php
@@ -114,6 +114,7 @@ class Controller_Handler_Sign extends Controller_Handler
         $email = $_GET["openid_sreg_email"];
 
         $id = 0;
+        $error = null;
         try {
             if ($isStudent) {
                 $globalKey = 'st-' . str_pad(str_replace('st-', '', $globalKey), 9, '0', STR_PAD_LEFT);
@@ -122,7 +123,9 @@ class Controller_Handler_Sign extends Controller_Handler
                 $id = User::instance()->signInByOpenID($globalKey);
             }
         } catch (Exception $e) {
-
+            $error = $e->getMessage();
+        } finally {
+            Model_Logs::logSigninDetailed($id, $isStaff, $isStudent, $globalKey, $error);
         }
 
         if ($id <= 0) {
diff --git a/~dev_rating/application/classes/Model/Logs.php b/~dev_rating/application/classes/Model/Logs.php
index ba318091d..f1a0c82ab 100644
--- a/~dev_rating/application/classes/Model/Logs.php
+++ b/~dev_rating/application/classes/Model/Logs.php
@@ -28,4 +28,17 @@ class Model_Logs extends Model
         return $result;
     }
 
+    public static function logSigninDetailed($accountID, $isStaff, $isStudent, $globalKey, $errorMessage) {
+        $query = 'SELECT * FROM log_sigindetailed(:account, :isstaff, :isstudent, :globalkey, :errormessage)';
+
+        $result = DB::query(Database::UPDATE, $query)
+            ->param(':account', $accountID)
+            ->param(':isstaff', $isStaff)
+            ->param(':isstudent', $isStudent)
+            ->param(':globalkey', $globalKey)
+            ->param(':errormessage', $errorMessage)
+            ->execute();
+        return $result;
+    }
+
 }
-- 
GitLab