+ public synchronized Name getPreferredName() {
+ return preferredName;
+ }
+
+ public synchronized void setPreferredName(Name preferred) throws GigiApiException {
+ if (preferred.getOwner() != this) {
+ throw new GigiApiException("Cannot set a name as preferred one that does not belong to this account.");
+ }
+ this.preferredName = preferred;
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET `preferredName`=? WHERE `id`=?")) {
+ ps.setInt(1, preferred.getId());
+ ps.setInt(2, getId());
+ ps.executeUpdate();
+ }
+
+ }
+
+ public boolean isInGroup(Group g) {
+ return groups.contains(g);
+ }
+
+ public Set<Group> getGroups() {
+ return Collections.unmodifiableSet(groups);
+ }
+
+ public void grantGroup(User granter, Group toGrant) throws GigiApiException {
+ if (toGrant.isManagedBySupport() && !granter.isInGroup(Group.SUPPORTER)) {
+ throw new GigiApiException("Group may only be managed by supporter");
+ }
+ if (toGrant.isManagedBySupport() && granter == this) {
+ throw new GigiApiException("Group may only be managed by supporter that is not oneself");
+ }
+ groups.add(toGrant);
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
+ ps.setInt(1, getId());
+ ps.setEnum(2, toGrant);
+ ps.setInt(3, granter.getId());
+ ps.execute();
+ }
+ }
+
+ public void revokeGroup(User revoker, Group toRevoke) throws GigiApiException {
+ if (toRevoke.isManagedBySupport() && !revoker.isInGroup(Group.SUPPORTER)) {
+ throw new GigiApiException("Group may only be managed by supporter");
+ }
+ groups.remove(toRevoke);
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `user_groups` SET `deleted`=CURRENT_TIMESTAMP, `revokedby`=? WHERE `deleted` IS NULL AND `permission`=?::`userGroup` AND `user`=?")) {
+ ps.setInt(1, revoker.getId());
+ ps.setEnum(2, toRevoke);
+ ps.setInt(3, getId());
+ ps.execute();
+ }
+ }
+
+ public List<Organisation> getOrganisations() {
+ return getOrganisations(false);
+ }
+
+ public List<Organisation> getOrganisations(boolean isAdmin) {
+ List<Organisation> orgas = new ArrayList<>();
+ try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL" + (isAdmin ? " AND master='y'" : ""))) {
+ query.setInt(1, getId());
+ try (GigiResultSet res = query.executeQuery()) {
+ while (res.next()) {
+ orgas.add(Organisation.getById(res.getInt(1)));
+ }
+
+ return orgas;
+ }
+ }
+ }
+
+ public static synchronized User getById(int id) {
+ CertificateOwner co = CertificateOwner.getById(id);
+ if (co instanceof User) {
+ return (User) co;
+ }
+
+ return null;
+ }
+
+ public static User getByEmail(String mail) {
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
+ ps.setString(1, mail);
+ GigiResultSet rs = ps.executeQuery();
+ if ( !rs.next()) {
+ return null;
+ }
+
+ return User.getById(rs.getInt(1));
+ }
+ }
+
+ public static User[] findByEmail(String mail) {
+ LinkedList<User> results = new LinkedList<User>();
+ 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")) {
+ ps.setString(1, mail);
+ GigiResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ results.add(User.getById(rs.getInt(1)));
+ }
+ return results.toArray(new User[results.size()]);
+ }
+ }
+
+ public EmailAddress[] getEmails() {
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `emails` WHERE `memid`=? AND `deleted` IS NULL")) {
+ ps.setInt(1, getId());
+
+ GigiResultSet rs = ps.executeQuery();
+ LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
+
+ while (rs.next()) {
+ data.add(EmailAddress.getById(rs.getInt(1)));
+ }
+
+ return data.toArray(new EmailAddress[0]);
+ }