Odyssey
2017053100_hcumbrlogintrack.php
1 <?php
2 /* Notes:
3  * 1. Anything complicated needs to be done with SQL.
4  * 2. Always have a check to know if the migration needs to occur (up or down).
5  * 3. Use up() and down(), not change(), because of the SQL
6  */
7 use Phinx\Migration\AbstractMigration;
8 
9 class HCUMbrLoginTrack extends AbstractMigration
10 {
11  /* This is the process for moving forward. We are re-creating the function
12  * drop existing function, create new
13  * rename the temporary table.
14  */
15  public function up()
16  {
17  // announce it
18  print "Converting hcumbrlogintrack\n";
19 
20  // create the SQL for the temp table
21  $sql = "
22 CREATE OR REPLACE FUNCTION hcumbrlogintrack(cucode character, member character, mustchange character, pwlastchange timestamp with time zone, product character, usermfaquest character varying, OUT logtime character) RETURNS character
23  LANGUAGE plpgsql
24  AS $$
25  DECLARE
26  tz char(25);
27  ym char(6);
28  retries integer;
29  grace integer;
30  pwdays integer;
31  results record;
32  rec_affected int;
33  logtime char(20);
34  homecuuserstable varchar(24);
35  BEGIN
36  /*
37  * SET CU USERS TABLE VALUE
38  */
39  homecuuserstable := lower(cucode) || 'user';
40  /*
41  * ***********
42  * Get cuadmin record for given credit union
43  */
44  SELECT INTO results * FROM cuadmin WHERE cu = cucode;
45  IF results.tz is null OR trim(results.tz) = '' THEN
46  tz:='US/Mountain';
47  ELSIF position('/' IN results.tz) > 0 THEN
48  tz:=results.tz;
49  ELSE
50  tz:='US/' || results.tz;
51  END IF;
52  IF results.retrylimit = 0 OR results.retrylimit IS NULL THEN
53  retries:=5;
54  ELSE
55  retries:=results.retrylimit;
56  END IF;
57  IF results.gracelimit = 0 OR results.gracelimit IS NULL THEN
58  grace:=5;
59  ELSE
60  grace:=results.gracelimit;
61  END IF;
62  IF results.pwdays IS NULL THEN
63  pwdays:=0;
64  ELSE
65  pwdays:=results.pwdays;
66  END IF;
67  EXECUTE 'set time zone ' || quote_literal(tz);
68  logtime:=to_char(now(),'YYYY/MM/DD HH24:MI:SS');
69  ym:=to_char(now(),'YYYYMM');
70  /*
71  * CHECK FOR EXISTENCE OF TABLE IN PG TABLE?
72  */
73  IF EXISTS (SELECT relname FROM pg_class WHERE relname = quote_ident(homecuuserstable))
74  THEN
75  IF mustchange='Y'
76  THEN
77  EXECUTE FORMAT('UPDATE %I '
78  || 'SET failedremain=%L, '
79  || ' priorlogin=lastlogin, '
80  || ' forceremain=(forceremain - 1), '
81  || ' lastlogin=%L, '
82  || ' challenge_quest_id=%L, '
83  || ' mfaquest=%L '
84  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 0, userMfaQuest, member);
85 
86  ELSE
87  IF pwdays > 0 AND pwlastchange < (now()- \"interval\"(pwdays || ' days')) THEN
88  EXECUTE FORMAT('UPDATE %I '
89  || 'SET failedremain=%L, '
90  || ' priorlogin=lastlogin, '
91  || ' lastlogin=%L, '
92  || ' forcechange=%L, '
93  || ' forceremain=%L, '
94  || ' challenge_quest_id=%L, '
95  || ' mfaquest=%L '
96  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 'Y', grace, 0, userMfaQuest, member);
97  ELSE
98  EXECUTE FORMAT('UPDATE %I '
99  || 'SET failedremain=%L, '
100  || ' priorlogin=lastlogin, '
101  || ' lastlogin=%L, '
102  || ' challenge_quest_id=%L, '
103  || ' mfaquest=%L '
104  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 0, userMfaQuest, member);
105  END IF;
106  END IF;
107  ELSE
108  /* ** ERROR -- SHOULDN'T BE HERE. */
109  END IF;
110 
111  EXECUTE 'set time zone ' || quote_literal('US/Mountain');
112 END;$$;
113  ";
114 
115  // create or update function
116  $rows = $this->query( $sql );
117 
118  }
119 
120  /* To execute this you need to run the following command:
121  * php vendor/bin/phinx rollback (using correct path to phinx)
122  * Or, to roll back to a specific time:
123  * php vendor/bin/phinx rollback -t 20151123211207
124  */
125  public function down()
126  {
127  // announce it
128  print "Converting hcumbrlogintrack\n";
129 
130  // create the SQL for the temp table
131  $sql = "
132 CREATE OR REPLACE FUNCTION hcumbrlogintrack(cucode character, member character, mustchange character, pwlastchange timestamp with time zone, product character, usermfaquest character varying, OUT logtime character) RETURNS character
133  LANGUAGE plpgsql
134  AS $$
135  DECLARE
136  tz char(25);
137  ym char(6);
138  retries integer;
139  grace integer;
140  pwdays integer;
141  results record;
142  rec_affected int;
143  logtime char(20);
144  homecuuserstable varchar(24);
145  BEGIN
146  /*
147  * SET CU USERS TABLE VALUE
148  */
149  homecuuserstable := lower(cucode) || 'user';
150  /*
151  * ***********
152  * Get cuadmin record for given credit union
153  */
154  SELECT INTO results * FROM cuadmin WHERE cu = cucode;
155  IF results.tz is null OR trim(results.tz) = '' THEN
156  tz:='US/Mountain';
157  ELSIF position('/' IN results.tz) > 0 THEN
158  tz:=results.tz;
159  ELSE
160  tz:='US/' || results.tz;
161  END IF;
162  IF results.retrylimit = 0 OR results.retrylimit IS NULL THEN
163  retries:=5;
164  ELSE
165  retries:=results.retrylimit;
166  END IF;
167  IF results.gracelimit = 0 OR results.gracelimit IS NULL THEN
168  grace:=5;
169  ELSE
170  grace:=results.gracelimit;
171  END IF;
172  IF results.pwdays IS NULL THEN
173  pwdays:=0;
174  ELSE
175  pwdays:=results.pwdays;
176  END IF;
177  EXECUTE 'set time zone ' || quote_literal(tz);
178  logtime:=to_char(now(),'YYYY/MM/DD HH24:MI:SS');
179  ym:=to_char(now(),'YYYYMM');
180  UPDATE culogtrack SET logins=logins +1
181  WHERE cu=cucode AND accountnumber=member AND yearmo=ym AND hcucode=product;
182  get diagnostics rec_affected = ROW_COUNT;
183  IF rec_affected = 0 THEN
184  INSERT INTO culogtrack values (cucode, member, ym, 1, product);
185  END IF;
186  /*
187  * CHECK FOR EXISTENCE OF TABLE IN PG TABLE?
188  */
189  IF EXISTS (SELECT relname FROM pg_class WHERE relname = quote_ident(homecuuserstable))
190  THEN
191  IF mustchange='Y'
192  THEN
193  EXECUTE FORMAT('UPDATE %I '
194  || 'SET failedremain=%L, '
195  || ' priorlogin=lastlogin, '
196  || ' forceremain=(forceremain - 1), '
197  || ' lastlogin=%L, '
198  || ' challenge_quest_id=%L, '
199  || ' mfaquest=%L '
200  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 0, userMfaQuest, member);
201 
202  ELSE
203  IF pwdays > 0 AND pwlastchange < (now()- \"interval\"(pwdays || ' days')) THEN
204  EXECUTE FORMAT('UPDATE %I '
205  || 'SET failedremain=%L, '
206  || ' priorlogin=lastlogin, '
207  || ' lastlogin=%L, '
208  || ' forcechange=%L, '
209  || ' forceremain=%L, '
210  || ' challenge_quest_id=%L, '
211  || ' mfaquest=%L '
212  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 'Y', grace, 0, userMfaQuest, member);
213  ELSE
214  EXECUTE FORMAT('UPDATE %I '
215  || 'SET failedremain=%L, '
216  || ' priorlogin=lastlogin, '
217  || ' lastlogin=%L, '
218  || ' challenge_quest_id=%L, '
219  || ' mfaquest=%L '
220  || 'WHERE user_name = %L;', homecuuserstable, retries, logtime, 0, userMfaQuest, member);
221  END IF;
222  END IF;
223  ELSE
224  /* ** ERROR -- SHOULDN'T BE HERE. */
225  END IF;
226 
227  EXECUTE 'set time zone ' || quote_literal('US/Mountain');
228 END;$$;
229  ";
230 
231  // create or update function
232  $rows = $this->query( $sql );
233 
234  }
235 }