|
36 | 36 | import com.appirio.tech.core.service.identity.representation.Achievement; |
37 | 37 | import com.appirio.tech.core.service.identity.representation.Country; |
38 | 38 | import com.appirio.tech.core.service.identity.representation.Credential; |
| 39 | +import com.appirio.tech.core.service.identity.representation.User2fa; |
39 | 40 | import com.appirio.tech.core.service.identity.representation.Email; |
40 | 41 | import com.appirio.tech.core.service.identity.representation.GroupMembership; |
41 | 42 | import com.appirio.tech.core.service.identity.representation.ProviderType; |
@@ -96,47 +97,109 @@ public abstract class UserDAO implements DaoBase<User>, Transactional<UserDAO> { |
96 | 97 | @RegisterMapperFactory(TCBeanMapperFactory.class) |
97 | 98 | @SqlQuery( |
98 | 99 | "SELECT " + USER_COLUMNS + ", " + |
99 | | - "s.password AS credential$encodedPassword, e.address AS email, e.status_id AS emailStatus " + |
| 100 | + "s.password AS credential$encodedPassword, e.address AS email, e.status_id AS emailStatus, " + |
| 101 | + "mfa.enabled AS mfaEnabled, mfa.verified AS mfaVerified " + |
100 | 102 | "FROM common_oltp.user AS u " + |
101 | 103 | "LEFT OUTER JOIN common_oltp.email AS e ON u.user_id = e.user_id AND e.email_type_id = 1 AND e.primary_ind = 1 " + |
102 | 104 | "LEFT OUTER JOIN common_oltp.security_user AS s ON u.user_id = s.login_id " + |
| 105 | + "LEFT JOIN common_oltp.user_2fa mfa ON mfa.user_id = u.user_id " + |
103 | 106 | "WHERE u.user_id = :id" |
104 | 107 | ) |
105 | 108 | public abstract User findUserById(@Bind("id") long id); |
106 | 109 |
|
107 | 110 | @RegisterMapperFactory(TCBeanMapperFactory.class) |
108 | 111 | @SqlQuery( |
109 | 112 | "SELECT " + USER_COLUMNS + ", " + |
110 | | - "e.address AS email, e.status_id AS emailStatus " + |
| 113 | + "e.address AS email, e.status_id AS emailStatus, " + |
| 114 | + "mfa.enabled AS mfaEnabled, mfa.verified AS mfaVerified " + |
111 | 115 | "FROM common_oltp.user AS u " + |
112 | 116 | "LEFT OUTER JOIN common_oltp.email AS e ON u.user_id = e.user_id AND e.email_type_id = 1 " + |
| 117 | + "LEFT JOIN common_oltp.user_2fa mfa ON mfa.user_id = u.user_id " + |
113 | 118 | "WHERE u.handle_lower = LOWER(:handle)" |
114 | 119 | ) |
115 | 120 | public abstract User findUserByHandle(@Bind("handle") String handle); |
116 | 121 |
|
117 | 122 | @RegisterMapperFactory(TCBeanMapperFactory.class) |
118 | 123 | @SqlQuery( |
119 | 124 | "SELECT " + USER_COLUMNS + ", " + |
120 | | - "e.address AS email, e.status_id AS emailStatus " + |
| 125 | + "e.address AS email, e.status_id AS emailStatus, " + |
| 126 | + "mfa.enabled AS mfaEnabled, mfa.verified AS mfaVerified " + |
121 | 127 | "FROM common_oltp.user AS u JOIN common_oltp.email AS e ON e.user_id = u.user_id " + |
| 128 | + "LEFT JOIN common_oltp.user_2fa mfa ON mfa.user_id = u.user_id " + |
122 | 129 | "WHERE LOWER(e.address) = LOWER(:email)" |
123 | 130 | ) |
124 | 131 | public abstract List<User> findUsersByEmail(@Bind("email") String email); |
125 | 132 |
|
| 133 | + @RegisterMapperFactory(TCBeanMapperFactory.class) |
| 134 | + @SqlQuery( |
| 135 | + "SELECT mfa.id AS id, u.user_id AS userId, u.handle AS handle, u.first_name AS firstName, e.address AS email, mfa.enabled AS enabled, mfa.verified AS verified " + |
| 136 | + "FROM common_oltp.user AS u JOIN common_oltp.email AS e ON e.user_id = u.user_id " + |
| 137 | + "LEFT JOIN common_oltp.user_2fa AS mfa ON mfa.user_id = u.user_id " + |
| 138 | + "WHERE LOWER(e.address) = LOWER(:email)" |
| 139 | + ) |
| 140 | + public abstract List<User2fa> findUser2faByEmail(@Bind("email") String email); |
| 141 | + |
| 142 | + @RegisterMapperFactory(TCBeanMapperFactory.class) |
| 143 | + @SqlQuery( |
| 144 | + "SELECT mfa.id AS id, u.user_id AS userId, u.handle AS handle, u.first_name AS firstName, e.address AS email, mfa.enabled AS enabled, mfa.verified AS verified " + |
| 145 | + "FROM common_oltp.user AS u LEFT JOIN common_oltp.email AS e ON e.user_id = u.user_id " + |
| 146 | + "LEFT JOIN common_oltp.user_2fa AS mfa ON mfa.user_id = u.user_id " + |
| 147 | + "WHERE u.user_id = :userId" |
| 148 | + ) |
| 149 | + public abstract User2fa findUser2faById(@Bind("userId") long userId); |
| 150 | + |
| 151 | + @SqlUpdate( |
| 152 | + "INSERT INTO common_oltp.user_2fa " + |
| 153 | + "(user_id, enabled) VALUES " + |
| 154 | + "(:userId, :enabled)") |
| 155 | + public abstract int insertUser2fa(@Bind("userId") long userId, @Bind("enabled") boolean enabled); |
| 156 | + |
| 157 | + @SqlUpdate( |
| 158 | + "UPDATE common_oltp.user_2fa SET " + |
| 159 | + "enabled=:enabled, " + |
| 160 | + "verified=:verified " + |
| 161 | + "WHERE id=:id") |
| 162 | + public abstract int update2fa(@Bind("id") long id, @Bind("enabled") boolean enabled, @Bind("verified") boolean verified); |
| 163 | + |
| 164 | + @SqlUpdate( |
| 165 | + "UPDATE common_oltp.user_2fa SET " + |
| 166 | + "enabled=:enabled, " + |
| 167 | + "verified=:verified " + |
| 168 | + "WHERE user_id=:userId") |
| 169 | + public abstract int update2faByUserId(@Bind("userId") long userId, @Bind("enabled") boolean enabled, @Bind("verified") boolean verified); |
| 170 | + |
| 171 | + @SqlUpdate( |
| 172 | + "UPDATE common_oltp.user_2fa SET " + |
| 173 | + "otp=:otp, " + |
| 174 | + "otp_expire=current_timestamp + (:duration ||' minutes')::interval " + |
| 175 | + "WHERE id=:id") |
| 176 | + public abstract int update2faOtp(@Bind("id") long id, @Bind("otp") String otp, @Bind("duration") int duration); |
| 177 | + |
| 178 | + @SqlQuery( |
| 179 | + "UPDATE common_oltp.user_2fa x SET otp=null, otp_expire=null " + |
| 180 | + "FROM (SELECT id, otp, otp_expire FROM common_oltp.user_2fa WHERE user_id=:userId FOR UPDATE)y " + |
| 181 | + "WHERE x.id=y.id " + |
| 182 | + "RETURNING CASE WHEN y.otp=:otp and y.otp_expire > current_timestamp THEN 1 ELSE 0 END") |
| 183 | + public abstract int verify2faOtp(@Bind("userId") long userId, @Bind("otp") String otp); |
| 184 | + |
126 | 185 | @RegisterMapperFactory(TCBeanMapperFactory.class) |
127 | 186 | @SqlQuery( |
128 | 187 | "SELECT " + USER_COLUMNS + ", " + |
129 | | - "e.address AS email, e.status_id AS emailStatus " + |
| 188 | + "e.address AS email, e.status_id AS emailStatus, " + |
| 189 | + "mfa.enabled AS mfaEnabled, mfa.verified AS mfaVerified " + |
130 | 190 | "FROM common_oltp.user AS u JOIN common_oltp.email AS e ON e.user_id = u.user_id " + |
| 191 | + "LEFT JOIN common_oltp.user_2fa AS mfa ON mfa.user_id = u.user_id " + |
131 | 192 | "WHERE e.address = :email" |
132 | 193 | ) |
133 | 194 | public abstract List<User> findUsersByEmailCS(@Bind("email") String email); |
134 | 195 |
|
135 | 196 | @RegisterMapperFactory(TCBeanMapperFactory.class) |
136 | 197 | @SqlQuery( |
137 | 198 | "SELECT " + USER_COLUMNS + ", " + |
138 | | - "e.address AS email, e.status_id AS emailStatus " + |
| 199 | + "e.address AS email, e.status_id AS emailStatus, " + |
| 200 | + "mfa.enabled AS mfaEnabled, mfa.verified AS mfaVerified " + |
139 | 201 | "FROM common_oltp.user AS u " + |
| 202 | + "LEFT JOIN common_oltp.user_2fa AS mfa ON mfa.user_id = u.user_id " + |
140 | 203 | "<joinOnEmail> common_oltp.email AS e ON u.user_id = e.user_id AND e.primary_ind = 1 " + |
141 | 204 | "<condition> " + |
142 | 205 | "<order> " + |
@@ -364,6 +427,22 @@ public User findUserByEmail(String email) { |
364 | 427 | // nothing matched with email parameter in the result, returns the first one. |
365 | 428 | return users.get(0); |
366 | 429 | } |
| 430 | + |
| 431 | + public User2fa findUserCredentialByEmail(String email) { |
| 432 | + List<User2fa> users = findUser2faByEmail(email); |
| 433 | + if(users==null || users.size()==0) |
| 434 | + return null; |
| 435 | + |
| 436 | + if(users.size()==1) |
| 437 | + return users.get(0); |
| 438 | + |
| 439 | + for (User2fa user : users) { |
| 440 | + if(user.getEmail().equals(email)) |
| 441 | + return user; |
| 442 | + } |
| 443 | + |
| 444 | + return users.get(0); |
| 445 | + } |
367 | 446 |
|
368 | 447 | /** |
369 | 448 | * |
|
0 commit comments