]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/User.java
69b76ad2004ec9aa24c5401b7d8f1f0f42845c12
[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) {
72         super(rs.getInt("id"));
73         updateName(rs);
74     }
75
76     private void updateName(GigiResultSet rs) {
77         dob = new DayDate(rs.getDate("dob"));
78         email = rs.getString("email");
79         preferredName = Name.getById(rs.getInt("preferredName"));
80
81         try {
82             if (rs.getString("Country") != null) {
83                 residenceCountry = Country.getCountryByCode(rs.getString("Country"), Country.CountryCodeType.CODE_2_CHARS);
84             }
85         } catch (GigiApiException e) {
86             throw new Error(e);
87         }
88
89         String localeStr = rs.getString("language");
90         if (localeStr == null || localeStr.equals("")) {
91             locale = Locale.getDefault();
92         } else {
93             locale = Language.getLocaleFromString(localeStr);
94         }
95
96         refreshGroups();
97     }
98
99     public synchronized void refreshGroups() {
100         HashSet<Group> hs = new HashSet<>();
101         try (GigiPreparedStatement psg = new GigiPreparedStatement("SELECT `permission` FROM `user_groups` WHERE `user`=? AND `deleted` is NULL")) {
102             psg.setInt(1, getId());
103
104             try (GigiResultSet rs2 = psg.executeQuery()) {
105                 while (rs2.next()) {
106                     hs.add(Group.getByString(rs2.getString(1)));
107                 }
108             }
109         }
110         groups = hs;
111     }
112
113     public User(String email, String password, DayDate dob, Locale locale, Country residenceCountry, NamePart... preferred) throws GigiApiException {
114         this.email = email;
115         this.dob = dob;
116         this.locale = locale;
117         this.preferredName = new Name(this, preferred);
118         try (GigiPreparedStatement query = new GigiPreparedStatement("INSERT INTO `users` SET `email`=?, `password`=?, `dob`=?, `language`=?, id=?, `preferredName`=?, `country` = ?")) {
119             query.setString(1, email);
120             query.setString(2, PasswordHash.hash(password));
121             query.setDate(3, dob.toSQLDate());
122             query.setString(4, locale.toString());
123             query.setInt(5, getId());
124             query.setInt(6, preferredName.getId());
125             query.setString(7, residenceCountry == null ? null : residenceCountry.getCode());
126             query.execute();
127         }
128         new EmailAddress(this, email, locale);
129     }
130
131     public Name[] getNames() {
132         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `id` FROM `names` WHERE `uid`=? AND `deleted` IS NULL", true)) {
133             return fetchNamesToArray(gps);
134         }
135     }
136
137     public Name[] getNonDeprecatedNames() {
138         try (GigiPreparedStatement gps = new GigiPreparedStatement("SELECT `id` FROM `names` WHERE `uid`=? AND `deleted` IS NULL AND `deprecated` IS NULL", true)) {
139             return fetchNamesToArray(gps);
140         }
141     }
142
143     private Name[] fetchNamesToArray(GigiPreparedStatement gps) {
144         gps.setInt(1, getId());
145         GigiResultSet rs = gps.executeQuery();
146         rs.last();
147         Name[] dt = new Name[rs.getRow()];
148         rs.beforeFirst();
149         for (int i = 0; rs.next(); i++) {
150             dt[i] = Name.getById(rs.getInt(1));
151         }
152         return dt;
153     }
154
155     public DayDate getDoB() {
156         return dob;
157     }
158
159     public void setDoB(DayDate dob) throws GigiApiException {
160         synchronized (Notary.class) {
161             if (getReceivedAssurances().length != 0) {
162                 throw new GigiApiException("No change after verification allowed.");
163             }
164
165             if ( !CalendarUtil.isOfAge(dob, User.MINIMUM_AGE)) {
166                 throw new GigiApiException("Entered date of birth is below the restricted age requirements.");
167             }
168
169             if (CalendarUtil.isOfAge(dob, User.MAXIMUM_PLAUSIBLE_AGE)) {
170                 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.");
171             }
172             this.dob = dob;
173             rawUpdateUserData();
174         }
175
176     }
177
178     protected void setDoBAsSupport(DayDate dob) throws GigiApiException {
179         synchronized (Notary.class) {
180             this.dob = dob;
181             rawUpdateUserData();
182         }
183
184     }
185
186     public String getEmail() {
187         return email;
188     }
189
190     public void changePassword(String oldPass, String newPass) throws GigiApiException {
191         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password` FROM `users` WHERE `id`=?")) {
192             ps.setInt(1, getId());
193             try (GigiResultSet rs = ps.executeQuery()) {
194                 if ( !rs.next()) {
195                     throw new GigiApiException("User not found... very bad.");
196                 }
197                 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
198                     throw new GigiApiException("Old password does not match.");
199                 }
200             }
201         }
202         setPassword(newPass);
203     }
204
205     private void setPassword(String newPass) throws GigiApiException {
206         PasswordStrengthChecker.assertStrongPassword(newPass, getNames(), getEmail());
207         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET `password`=? WHERE id=?")) {
208             ps.setString(1, PasswordHash.hash(newPass));
209             ps.setInt(2, getId());
210             ps.executeUpdate();
211         }
212     }
213
214     public boolean canAssure() {
215         if (POJAM_ENABLED) {
216             if ( !CalendarUtil.isOfAge(dob, POJAM_AGE)) { // PoJAM
217                 return false;
218             }
219         } else {
220             if ( !CalendarUtil.isOfAge(dob, ADULT_AGE)) {
221                 return false;
222             }
223         }
224         if (getAssurancePoints() < 100) {
225             return false;
226         }
227
228         return hasPassedCATS();
229
230     }
231
232     public boolean hasPassedCATS() {
233         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` where `user_id`=? AND `variant_id`=?")) {
234             query.setInt(1, getId());
235             query.setInt(2, CATSType.ASSURER_CHALLENGE.getId());
236             try (GigiResultSet rs = query.executeQuery()) {
237                 if (rs.next()) {
238                     return true;
239                 } else {
240                     return false;
241                 }
242             }
243         }
244     }
245
246     public int getAssurancePoints() {
247         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT SUM(lastpoints) FROM ( SELECT DISTINCT ON (`from`) `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`, `when` DESC) as p")) {
248             query.setInt(1, getId());
249
250             GigiResultSet rs = query.executeQuery();
251             int points = 0;
252
253             if (rs.next()) {
254                 points = rs.getInt(1);
255             }
256
257             return points;
258         }
259     }
260
261     public int getExperiencePoints() {
262         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")) {
263             query.setInt(1, getId());
264             query.setString(2, AssuranceType.FACE_TO_FACE.getDescription());
265
266             GigiResultSet rs = query.executeQuery();
267             int points = 0;
268
269             if (rs.next()) {
270                 points = rs.getInt(1) * EXPERIENCE_POINTS;
271             }
272
273             return points;
274         }
275     }
276
277     /**
278      * Gets the maximum allowed points NOW. Note that an assurance needs to
279      * re-check PoJam as it has taken place in the past.
280      * 
281      * @return the maximal points @
282      */
283     @SuppressWarnings("unused")
284     public int getMaxAssurePoints() {
285         if ( !CalendarUtil.isOfAge(dob, ADULT_AGE) && POJAM_ENABLED) {
286             return 10; // PoJAM
287         }
288
289         int exp = getExperiencePoints();
290         int points = 10;
291
292         if (exp >= 5 * EXPERIENCE_POINTS) {
293             points += 5;
294         }
295         if (exp >= 10 * EXPERIENCE_POINTS) {
296             points += 5;
297         }
298         if (exp >= 15 * EXPERIENCE_POINTS) {
299             points += 5;
300         }
301         if (exp >= 20 * EXPERIENCE_POINTS) {
302             points += 5;
303         }
304         if (exp >= 25 * EXPERIENCE_POINTS) {
305             points += 5;
306         }
307
308         return points;
309     }
310
311     public boolean isValidName(String name) {
312         for (Name n : getNames()) {
313             if (n.matches(name) && n.getAssurancePoints() >= 50) {
314                 return true;
315             }
316         }
317         return false;
318     }
319
320     public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
321         for (EmailAddress email : getEmails()) {
322             if (email.getAddress().equals(newMail.getAddress())) {
323                 if ( !email.isVerified()) {
324                     throw new GigiApiException("Email not verified.");
325                 }
326
327                 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET email=? WHERE id=?")) {
328                     ps.setString(1, newMail.getAddress());
329                     ps.setInt(2, getId());
330                     ps.execute();
331                 }
332
333                 this.email = newMail.getAddress();
334                 return;
335             }
336         }
337
338         throw new GigiApiException("Given address not an address of the user.");
339     }
340
341     public void deleteEmail(EmailAddress delMail) throws GigiApiException {
342         if (getEmail().equals(delMail.getAddress())) {
343             throw new GigiApiException("Can't delete user's default e-mail.");
344         }
345
346         for (EmailAddress email : getEmails()) {
347             if (email.getId() == delMail.getId()) {
348                 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `emails` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
349                     ps.setInt(1, delMail.getId());
350                     ps.execute();
351                 }
352                 return;
353             }
354         }
355         throw new GigiApiException("Email not one of user's email addresses.");
356     }
357
358     public synchronized Assurance[] getReceivedAssurances() {
359         if (receivedAssurances == null) {
360             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")) {
361                 query.setInt(1, getId());
362
363                 GigiResultSet res = query.executeQuery();
364                 List<Assurance> assurances = new LinkedList<Assurance>();
365
366                 while (res.next()) {
367                     assurances.add(assuranceByRes(res));
368                 }
369
370                 this.receivedAssurances = assurances.toArray(new Assurance[0]);
371             }
372         }
373
374         return receivedAssurances;
375     }
376
377     public synchronized Assurance[] getMadeAssurances() {
378         if (madeAssurances == null) {
379             try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM notary WHERE `from`=? AND deleted is NULL ORDER BY `when` DESC")) {
380                 query.setInt(1, getId());
381
382                 try (GigiResultSet res = query.executeQuery()) {
383                     List<Assurance> assurances = new LinkedList<Assurance>();
384
385                     while (res.next()) {
386                         assurances.add(assuranceByRes(res));
387                     }
388
389                     this.madeAssurances = assurances.toArray(new Assurance[0]);
390                 }
391             }
392         }
393
394         return madeAssurances;
395     }
396
397     public synchronized void invalidateMadeAssurances() {
398         madeAssurances = null;
399     }
400
401     public synchronized void invalidateReceivedAssurances() {
402         receivedAssurances = null;
403     }
404
405     private void rawUpdateUserData() {
406         try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET dob=? WHERE id=?")) {
407             update.setDate(1, getDoB().toSQLDate());
408             update.setInt(2, getId());
409             update.executeUpdate();
410         }
411     }
412
413     public Locale getPreferredLocale() {
414         return locale;
415     }
416
417     public void setPreferredLocale(Locale locale) {
418         this.locale = locale;
419
420     }
421
422     public synchronized Name getPreferredName() {
423         return preferredName;
424     }
425
426     public synchronized void setPreferredName(Name preferred) throws GigiApiException {
427         if (preferred.getOwner() != this) {
428             throw new GigiApiException("Cannot set a name as preferred one that does not belong to this account.");
429         }
430         this.preferredName = preferred;
431         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET `preferredName`=? WHERE `id`=?")) {
432             ps.setInt(1, preferred.getId());
433             ps.setInt(2, getId());
434             ps.executeUpdate();
435         }
436
437     }
438
439     public boolean isInGroup(Group g) {
440         return groups.contains(g);
441     }
442
443     public Set<Group> getGroups() {
444         return Collections.unmodifiableSet(groups);
445     }
446
447     public void grantGroup(User granter, Group toGrant) throws GigiApiException {
448         if (toGrant.isManagedBySupport() && !granter.isInGroup(Group.SUPPORTER)) {
449             throw new GigiApiException("Group may only be managed by supporter");
450         }
451         groups.add(toGrant);
452         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
453             ps.setInt(1, getId());
454             ps.setString(2, toGrant.getDatabaseName());
455             ps.setInt(3, granter.getId());
456             ps.execute();
457         }
458     }
459
460     public void revokeGroup(User revoker, Group toRevoke) {
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.setString(2, toRevoke.getDatabaseName());
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));
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 }