]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/User.java
ad2d38675c64329da0eef5d6c2e16974c8e1c061
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashSet;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Locale;
12 import java.util.Set;
13
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
18 import org.cacert.gigi.dbObjects.CATS.CATSType;
19 import org.cacert.gigi.dbObjects.Country.CountryCodeType;
20 import org.cacert.gigi.localisation.Language;
21 import org.cacert.gigi.output.DateSelector;
22 import org.cacert.gigi.pages.PasswordResetPage;
23 import org.cacert.gigi.util.CalendarUtil;
24 import org.cacert.gigi.util.DayDate;
25 import org.cacert.gigi.util.Notary;
26 import org.cacert.gigi.util.PasswordHash;
27 import org.cacert.gigi.util.PasswordStrengthChecker;
28 import org.cacert.gigi.util.TimeConditions;
29
30 /**
31  * Represents an acting, assurable, user. Synchronizing on user means: no
32  * name-change and no assurance.
33  */
34 public class User extends CertificateOwner {
35
36     private static final long serialVersionUID = -7915843843752264176L;
37
38     private DayDate dob;
39
40     private String email;
41
42     private Assurance[] receivedAssurances;
43
44     private Assurance[] madeAssurances;
45
46     private Locale locale;
47
48     private Set<Group> groups = new HashSet<>();
49
50     public static final int MINIMUM_AGE = 16;
51
52     public static final int MAXIMUM_PLAUSIBLE_AGE = 120;
53
54     public static final int POJAM_AGE = 14;
55
56     public static final int ADULT_AGE = 18;
57
58     public static final boolean POJAM_ENABLED = false;
59
60     public static final int EXPERIENCE_POINTS = 4;
61
62     /**
63      * Time in months a verification is considered "recent".
64      */
65     public static final int VERIFICATION_MONTHS = TimeConditions.getInstance().getVerificationMonths();
66
67     private Name preferredName;
68
69     private Country residenceCountry;
70
71     protected User(GigiResultSet rs) throws GigiApiException {
72         super(rs.getInt("id"));
73
74         dob = new DayDate(rs.getDate("dob"));
75         email = rs.getString("email");
76         preferredName = Name.getById(rs.getInt("preferredName"));
77
78         if (rs.getString("country") != null) {
79             residenceCountry = Country.getCountryByCode(rs.getString("Country"), Country.CountryCodeType.CODE_2_CHARS);
80         }
81
82         String localeStr = rs.getString("language");
83         if (localeStr == null || localeStr.equals("")) {
84             locale = Locale.getDefault();
85         } else {
86             locale = Language.getLocaleFromString(localeStr);
87         }
88
89         refreshGroups();
90     }
91
92     public synchronized void refreshGroups() {
93         HashSet<Group> hs = new HashSet<>();
94         try (GigiPreparedStatement psg = new GigiPreparedStatement("SELECT `permission` FROM `user_groups` WHERE `user`=? AND `deleted` is NULL")) {
95             psg.setInt(1, getId());
96
97             try (GigiResultSet rs2 = psg.executeQuery()) {
98                 while (rs2.next()) {
99                     hs.add(Group.getByString(rs2.getString(1)));
100                 }
101             }
102         }
103         groups = hs;
104     }
105
106     public User(String email, String password, DayDate dob, Locale locale, Country residenceCountry, NamePart... preferred) throws GigiApiException {
107         this.email = email;
108         this.dob = dob;
109         this.locale = locale;
110         this.preferredName = new Name(this, preferred);
111         try (GigiPreparedStatement query = new GigiPreparedStatement("INSERT INTO `users` SET `email`=?, `password`=?, `dob`=?, `language`=?, id=?, `preferredName`=?, `country` = ?")) {
112             query.setString(1, email);
113             query.setString(2, PasswordHash.hash(password));
114             query.setDate(3, dob.toSQLDate());
115             query.setString(4, locale.toString());
116             query.setInt(5, getId());
117             query.setInt(6, preferredName.getId());
118             query.setString(7, residenceCountry == null ? null : residenceCountry.getCode());
119             query.execute();
120         }
121         new EmailAddress(this, email, locale);
122     }
123
124     public Name[] getNames() {
125         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `id` FROM `names` WHERE `uid`=? AND `deleted` IS NULL", true)) {
126             gps.setInt(1, getId());
127             return fetchNamesToArray(gps);
128         }
129     }
130
131     public Name[] getNonDeprecatedNames() {
132         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `id` FROM `names` WHERE `uid`=? AND `deleted` IS NULL AND `deprecated` IS NULL", true)) {
133             gps.setInt(1, getId());
134             return fetchNamesToArray(gps);
135         }
136     }
137
138     private Name[] fetchNamesToArray(GigiPreparedStatement gps) {
139         GigiResultSet rs = gps.executeQuery();
140         rs.last();
141         Name[] dt = new Name[rs.getRow()];
142         rs.beforeFirst();
143         for (int i = 0; rs.next(); i++) {
144             dt[i] = Name.getById(rs.getInt(1));
145         }
146         return dt;
147     }
148
149     public DayDate getDoB() {
150         return dob;
151     }
152
153     public void setDoB(DayDate dob) throws GigiApiException {
154         synchronized (Notary.class) {
155             if (getReceivedAssurances().length != 0) {
156                 throw new GigiApiException("No change after verification allowed.");
157             }
158
159             if ( !CalendarUtil.isOfAge(dob, User.MINIMUM_AGE)) {
160                 throw new GigiApiException("Entered date of birth is below the restricted age requirements.");
161             }
162
163             if (CalendarUtil.isOfAge(dob, User.MAXIMUM_PLAUSIBLE_AGE)) {
164                 throw new GigiApiException("Entered date of birth exceeds the maximum age set in our policies. Please check your DoB is correct and contact support if the issue persists.");
165             }
166             this.dob = dob;
167             rawUpdateUserData();
168         }
169
170     }
171
172     protected void setDoBAsSupport(DayDate dob) throws GigiApiException {
173         synchronized (Notary.class) {
174             this.dob = dob;
175             rawUpdateUserData();
176         }
177
178     }
179
180     public String getEmail() {
181         return email;
182     }
183
184     public void changePassword(String oldPass, String newPass) throws GigiApiException {
185         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password` FROM `users` WHERE `id`=?")) {
186             ps.setInt(1, getId());
187             try (GigiResultSet rs = ps.executeQuery()) {
188                 if ( !rs.next()) {
189                     throw new GigiApiException("User not found... very bad.");
190                 }
191                 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
192                     throw new GigiApiException("Old password does not match.");
193                 }
194             }
195         }
196         setPassword(newPass);
197     }
198
199     private void setPassword(String newPass) throws GigiApiException {
200         PasswordStrengthChecker.assertStrongPassword(newPass, getNames(), getEmail());
201         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET `password`=? WHERE id=?")) {
202             ps.setString(1, PasswordHash.hash(newPass));
203             ps.setInt(2, getId());
204             ps.executeUpdate();
205         }
206     }
207
208     public boolean canAssure() {
209         if (POJAM_ENABLED) {
210             if ( !CalendarUtil.isOfAge(dob, POJAM_AGE)) { // PoJAM
211                 return false;
212             }
213         } else {
214             if ( !CalendarUtil.isOfAge(dob, ADULT_AGE)) {
215                 return false;
216             }
217         }
218         if (getAssurancePoints() < 100) {
219             return false;
220         }
221
222         return hasPassedCATS();
223
224     }
225
226     public boolean hasPassedCATS() {
227         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` where `user_id`=? AND `variant_id`=?")) {
228             query.setInt(1, getId());
229             query.setInt(2, CATSType.ASSURER_CHALLENGE.getId());
230             try (GigiResultSet rs = query.executeQuery()) {
231                 if (rs.next()) {
232                     return true;
233                 } else {
234                     return false;
235                 }
236             }
237         }
238     }
239
240     public int getAssurancePoints() {
241         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT SUM(lastpoints) FROM ( SELECT DISTINCT ON (`from`, `method`) `from`, `points` as lastpoints FROM `notary` INNER JOIN `names` ON `names`.`id`=`to` WHERE `notary`.`deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP) AND `names`.`uid` = ? ORDER BY `from`, `method`, `when` DESC) as p")) {
242             query.setInt(1, getId());
243
244             GigiResultSet rs = query.executeQuery();
245             int points = 0;
246
247             if (rs.next()) {
248                 points = rs.getInt(1);
249             }
250
251             return points;
252         }
253     }
254
255     public int getExperiencePoints() {
256         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT count(*) FROM ( SELECT `names`.`uid` FROM `notary` INNER JOIN `names` ON `names`.`id` = `to` WHERE `from`=? AND `notary`.`deleted` IS NULL AND `method` = ? ::`notaryType` GROUP BY `names`.`uid`) as p")) {
257             query.setInt(1, getId());
258             query.setEnum(2, AssuranceType.FACE_TO_FACE);
259
260             GigiResultSet rs = query.executeQuery();
261             int points = 0;
262
263             if (rs.next()) {
264                 points = rs.getInt(1) * EXPERIENCE_POINTS;
265             }
266
267             return points;
268         }
269     }
270
271     /**
272      * Gets the maximum allowed points NOW. Note that an assurance needs to
273      * re-check PoJam as it has taken place in the past.
274      * 
275      * @return the maximal points @
276      */
277     @SuppressWarnings("unused")
278     public int getMaxAssurePoints() {
279         if ( !CalendarUtil.isOfAge(dob, ADULT_AGE) && POJAM_ENABLED) {
280             return 10; // PoJAM
281         }
282
283         int exp = getExperiencePoints();
284         int points = 10;
285
286         if (exp >= 5 * EXPERIENCE_POINTS) {
287             points += 5;
288         }
289         if (exp >= 10 * EXPERIENCE_POINTS) {
290             points += 5;
291         }
292         if (exp >= 15 * EXPERIENCE_POINTS) {
293             points += 5;
294         }
295         if (exp >= 20 * EXPERIENCE_POINTS) {
296             points += 5;
297         }
298         if (exp >= 25 * EXPERIENCE_POINTS) {
299             points += 5;
300         }
301
302         return points;
303     }
304
305     public boolean isValidName(String name) {
306         for (Name n : getNames()) {
307             if (n.matches(name) && n.getAssurancePoints() >= 50) {
308                 return true;
309             }
310         }
311         return false;
312     }
313
314     public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
315         for (EmailAddress email : getEmails()) {
316             if (email.getAddress().equals(newMail.getAddress())) {
317                 if ( !email.isVerified()) {
318                     throw new GigiApiException("Email not verified.");
319                 }
320
321                 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET email=? WHERE id=?")) {
322                     ps.setString(1, newMail.getAddress());
323                     ps.setInt(2, getId());
324                     ps.execute();
325                 }
326
327                 this.email = newMail.getAddress();
328                 return;
329             }
330         }
331
332         throw new GigiApiException("Given address not an address of the user.");
333     }
334
335     public void deleteEmail(EmailAddress delMail) throws GigiApiException {
336         if (getEmail().equals(delMail.getAddress())) {
337             throw new GigiApiException("Can't delete user's default e-mail.");
338         }
339
340         for (EmailAddress email : getEmails()) {
341             if (email.getId() == delMail.getId()) {
342                 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `emails` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
343                     ps.setInt(1, delMail.getId());
344                     ps.execute();
345                 }
346                 return;
347             }
348         }
349         throw new GigiApiException("Email not one of user's email addresses.");
350     }
351
352     public synchronized Assurance[] getReceivedAssurances() {
353         if (receivedAssurances == null) {
354             try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM `notary` INNER JOIN `names` ON `names`.`id` = `notary`.`to` WHERE `names`.`uid`=? AND `notary`.`deleted` IS NULL ORDER BY `when` DESC")) {
355                 query.setInt(1, getId());
356
357                 GigiResultSet res = query.executeQuery();
358                 List<Assurance> assurances = new LinkedList<Assurance>();
359
360                 while (res.next()) {
361                     assurances.add(assuranceByRes(res));
362                 }
363
364                 this.receivedAssurances = assurances.toArray(new Assurance[0]);
365             }
366         }
367
368         return receivedAssurances;
369     }
370
371     public synchronized Assurance[] getMadeAssurances() {
372         if (madeAssurances == null) {
373             try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM notary WHERE `from`=? AND deleted is NULL ORDER BY `when` DESC")) {
374                 query.setInt(1, getId());
375
376                 try (GigiResultSet res = query.executeQuery()) {
377                     List<Assurance> assurances = new LinkedList<Assurance>();
378
379                     while (res.next()) {
380                         assurances.add(assuranceByRes(res));
381                     }
382
383                     this.madeAssurances = assurances.toArray(new Assurance[0]);
384                 }
385             }
386         }
387
388         return madeAssurances;
389     }
390
391     public synchronized void invalidateMadeAssurances() {
392         madeAssurances = null;
393     }
394
395     public synchronized void invalidateReceivedAssurances() {
396         receivedAssurances = null;
397     }
398
399     private void rawUpdateUserData() {
400         try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET dob=? WHERE id=?")) {
401             update.setDate(1, getDoB().toSQLDate());
402             update.setInt(2, getId());
403             update.executeUpdate();
404         }
405     }
406
407     public Locale getPreferredLocale() {
408         return locale;
409     }
410
411     public void setPreferredLocale(Locale locale) {
412         this.locale = locale;
413
414     }
415
416     public synchronized Name getPreferredName() {
417         return preferredName;
418     }
419
420     public synchronized void setPreferredName(Name preferred) throws GigiApiException {
421         if (preferred.getOwner() != this) {
422             throw new GigiApiException("Cannot set a name as preferred one that does not belong to this account.");
423         }
424         this.preferredName = preferred;
425         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET `preferredName`=? WHERE `id`=?")) {
426             ps.setInt(1, preferred.getId());
427             ps.setInt(2, getId());
428             ps.executeUpdate();
429         }
430
431     }
432
433     public boolean isInGroup(Group g) {
434         return groups.contains(g);
435     }
436
437     public Set<Group> getGroups() {
438         return Collections.unmodifiableSet(groups);
439     }
440
441     public void grantGroup(User granter, Group toGrant) throws GigiApiException {
442         if (toGrant.isManagedBySupport() && !granter.isInGroup(Group.SUPPORTER)) {
443             throw new GigiApiException("Group may only be managed by supporter");
444         }
445         if (toGrant.isManagedBySupport() && granter == this) {
446             throw new GigiApiException("Group may only be managed by supporter that is not oneself");
447         }
448         groups.add(toGrant);
449         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
450             ps.setInt(1, getId());
451             ps.setEnum(2, toGrant);
452             ps.setInt(3, granter.getId());
453             ps.execute();
454         }
455     }
456
457     public void revokeGroup(User revoker, Group toRevoke) throws GigiApiException {
458         if (toRevoke.isManagedBySupport() && !revoker.isInGroup(Group.SUPPORTER)) {
459             throw new GigiApiException("Group may only be managed by supporter");
460         }
461         groups.remove(toRevoke);
462         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `user_groups` SET `deleted`=CURRENT_TIMESTAMP, `revokedby`=? WHERE `deleted` IS NULL AND `permission`=?::`userGroup` AND `user`=?")) {
463             ps.setInt(1, revoker.getId());
464             ps.setEnum(2, toRevoke);
465             ps.setInt(3, getId());
466             ps.execute();
467         }
468     }
469
470     public List<Organisation> getOrganisations() {
471         return getOrganisations(false);
472     }
473
474     public List<Organisation> getOrganisations(boolean isAdmin) {
475         List<Organisation> orgas = new ArrayList<>();
476         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL" + (isAdmin ? " AND master='y'" : ""))) {
477             query.setInt(1, getId());
478             try (GigiResultSet res = query.executeQuery()) {
479                 while (res.next()) {
480                     orgas.add(Organisation.getById(res.getInt(1)));
481                 }
482
483                 return orgas;
484             }
485         }
486     }
487
488     public static synchronized User getById(int id) {
489         CertificateOwner co = CertificateOwner.getById(id);
490         if (co instanceof User) {
491             return (User) co;
492         }
493
494         return null;
495     }
496
497     public static User getByEmail(String mail) {
498         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
499             ps.setString(1, mail);
500             GigiResultSet rs = ps.executeQuery();
501             if ( !rs.next()) {
502                 return null;
503             }
504
505             return User.getById(rs.getInt(1));
506         }
507     }
508
509     public static User[] findByEmail(String mail) {
510         LinkedList<User> results = new LinkedList<User>();
511         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `users`.`email` LIKE ? AND `deleted` IS NULL GROUP BY `users`.`id` LIMIT 100")) {
512             ps.setString(1, mail);
513             GigiResultSet rs = ps.executeQuery();
514             while (rs.next()) {
515                 results.add(User.getById(rs.getInt(1)));
516             }
517             return results.toArray(new User[results.size()]);
518         }
519     }
520
521     public EmailAddress[] getEmails() {
522         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `emails` WHERE `memid`=? AND `deleted` IS NULL")) {
523             ps.setInt(1, getId());
524
525             GigiResultSet rs = ps.executeQuery();
526             LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
527
528             while (rs.next()) {
529                 data.add(EmailAddress.getById(rs.getInt(1)));
530             }
531
532             return data.toArray(new EmailAddress[0]);
533         }
534     }
535
536     @Override
537     public boolean isValidEmail(String email) {
538         for (EmailAddress em : getEmails()) {
539             if (em.getAddress().equals(email)) {
540                 return em.isVerified();
541             }
542         }
543
544         return false;
545     }
546
547     public String[] getTrainings() {
548         try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `pass_date`, `type_text`, `language`, `version` FROM `cats_passed` LEFT JOIN `cats_type` ON `cats_type`.`id`=`cats_passed`.`variant_id`  WHERE `user_id`=? ORDER BY `pass_date` ASC")) {
549             prep.setInt(1, getId());
550             GigiResultSet res = prep.executeQuery();
551             List<String> entries = new LinkedList<String>();
552
553             while (res.next()) {
554                 StringBuilder training = new StringBuilder();
555                 training.append(DateSelector.getDateFormat().format(res.getTimestamp(1)));
556                 training.append(" (");
557                 training.append(res.getString(2));
558                 if (res.getString(3).length() > 0) {
559                     training.append(" ");
560                     training.append(res.getString(3));
561                 }
562                 if (res.getString(4).length() > 0) {
563                     training.append(", ");
564                     training.append(res.getString(4));
565                 }
566                 training.append(")");
567                 entries.add(training.toString());
568             }
569
570             return entries.toArray(new String[0]);
571         }
572
573     }
574
575     public int generatePasswordResetTicket(User actor, String token, String privateToken) {
576         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `passwordResetTickets` SET `memid`=?, `creator`=?, `token`=?, `private_token`=?")) {
577             ps.setInt(1, getId());
578             ps.setInt(2, getId());
579             ps.setString(3, token);
580             ps.setString(4, PasswordHash.hash(privateToken));
581             ps.execute();
582             return ps.lastInsertId();
583         }
584     }
585
586     public static User getResetWithToken(int id, String token) {
587         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid` FROM `passwordResetTickets` WHERE `id`=? AND `token`=? AND `used` IS NULL AND `created` > CURRENT_TIMESTAMP - interval '1 hours' * ?")) {
588             ps.setInt(1, id);
589             ps.setString(2, token);
590             ps.setInt(3, PasswordResetPage.HOUR_MAX);
591             GigiResultSet res = ps.executeQuery();
592             if ( !res.next()) {
593                 return null;
594             }
595             return User.getById(res.getInt(1));
596         }
597     }
598
599     public synchronized void consumePasswordResetTicket(int id, String private_token, String newPassword) throws GigiApiException {
600         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `private_token` FROM `passwordResetTickets` WHERE `id`=? AND `memid`=? AND `used` IS NULL")) {
601             ps.setInt(1, id);
602             ps.setInt(2, getId());
603             GigiResultSet rs = ps.executeQuery();
604             if ( !rs.next()) {
605                 throw new GigiApiException("Token could not be found, has already been used, or is expired.");
606             }
607             if (PasswordHash.verifyHash(private_token, rs.getString(1)) == null) {
608                 throw new GigiApiException("Private token does not match.");
609             }
610             setPassword(newPassword);
611         }
612         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `passwordResetTickets` SET  `used` = CURRENT_TIMESTAMP WHERE `id`=?")) {
613             ps.setInt(1, id);
614             ps.executeUpdate();
615         }
616     }
617
618     private Assurance assuranceByRes(GigiResultSet res) {
619         try {
620             return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), Name.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date"), res.getString("country") == null ? null : Country.getCountryByCode(res.getString("country"), CountryCodeType.CODE_2_CHARS), res.getTimestamp("expire"));
621         } catch (GigiApiException e) {
622             throw new Error(e);
623         }
624     }
625
626     public boolean isInVerificationLimit() {
627         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` INNER JOIN `names` ON `names`.`id`=`to` WHERE `names`.`uid` = ? AND `when` > (now() - (interval '1 month' * ?)) AND (`expire` IS NULL OR `expire` > now()) AND `notary`.`deleted` IS NULL;")) {
628             ps.setInt(1, getId());
629             ps.setInt(2, VERIFICATION_MONTHS);
630
631             GigiResultSet rs = ps.executeQuery();
632             return rs.next();
633         }
634     }
635
636     private void writeObject(ObjectOutputStream oos) throws IOException {}
637
638     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
639
640     public Country getResidenceCountry() {
641         return residenceCountry;
642     }
643
644     public void setResidenceCountry(Country residenceCountry) {
645         this.residenceCountry = residenceCountry;
646         rawUpdateCountryData();
647     }
648
649     private void rawUpdateCountryData() {
650         try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET country=? WHERE id=?")) {
651             update.setString(1, residenceCountry == null ? null : residenceCountry.getCode());
652             update.setInt(2, getId());
653             update.executeUpdate();
654         }
655     }
656 }