20
20
package repository
21
21
22
22
import (
23
+ "fmt"
23
24
"github.com/devtron-labs/devtron/api/bean"
24
25
userBean "github.com/devtron-labs/devtron/pkg/auth/user/bean"
26
+ "github.com/devtron-labs/devtron/pkg/auth/user/repository/helper"
27
+ "github.com/devtron-labs/devtron/pkg/auth/user/util"
25
28
"github.com/devtron-labs/devtron/pkg/sql"
26
29
"github.com/go-pg/pg"
27
30
"go.uber.org/zap"
@@ -59,13 +62,14 @@ func NewUserRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *User
59
62
}
60
63
61
64
type UserModel struct {
62
- TableName struct {} `sql:"users" pg:",discard_unknown_columns"`
63
- Id int32 `sql:"id,pk"`
64
- EmailId string `sql:"email_id,notnull"`
65
- AccessToken string `sql:"access_token"`
66
- Active bool `sql:"active,notnull"`
67
- UserType string `sql:"user_type"`
68
- UserAudit * UserAudit `sql:"-"`
65
+ TableName struct {} `sql:"users" pg:",discard_unknown_columns"`
66
+ Id int32 `sql:"id,pk"`
67
+ EmailId string `sql:"email_id,notnull"`
68
+ RequestEmailId string `sql:"request_email_id"`
69
+ AccessToken string `sql:"access_token"`
70
+ Active bool `sql:"active,notnull"`
71
+ UserType string `sql:"user_type"`
72
+ UserAudit * UserAudit `sql:"-"`
69
73
sql.AuditLog
70
74
}
71
75
@@ -79,6 +83,8 @@ type UserRoleModel struct {
79
83
}
80
84
81
85
func (impl UserRepositoryImpl ) CreateUser (userModel * UserModel , tx * pg.Tx ) (* UserModel , error ) {
86
+ userModel .RequestEmailId = userModel .EmailId
87
+ userModel .EmailId = util .ConvertEmailToLowerCase (userModel .EmailId )
82
88
err := tx .Insert (userModel )
83
89
if err != nil {
84
90
impl .Logger .Error (err )
@@ -88,6 +94,7 @@ func (impl UserRepositoryImpl) CreateUser(userModel *UserModel, tx *pg.Tx) (*Use
88
94
return userModel , nil
89
95
}
90
96
func (impl UserRepositoryImpl ) UpdateUser (userModel * UserModel , tx * pg.Tx ) (* UserModel , error ) {
97
+ userModel .EmailId = util .ConvertEmailToLowerCase (userModel .EmailId )
91
98
err := tx .Update (userModel )
92
99
if err != nil {
93
100
impl .Logger .Error (err )
@@ -117,6 +124,7 @@ func (impl UserRepositoryImpl) UpdateToInactiveByIds(ids []int32, tx *pg.Tx, log
117
124
func (impl UserRepositoryImpl ) GetById (id int32 ) (* UserModel , error ) {
118
125
var model UserModel
119
126
err := impl .dbConnection .Model (& model ).Where ("id = ?" , id ).Where ("active = ?" , true ).Select ()
127
+ model .EmailId = util .ConvertEmailToLowerCase (model .EmailId )
120
128
return & model , err
121
129
}
122
130
@@ -134,13 +142,14 @@ func (impl UserRepositoryImpl) GetEmailByIds(ids []int32) ([]string, error) {
134
142
for _ , model := range models {
135
143
userEmails = append (userEmails , model .EmailId )
136
144
}
137
- return userEmails , err
145
+ return util . ConvertEmailsToLowerCase ( userEmails ) , err
138
146
139
147
}
140
148
141
149
func (impl UserRepositoryImpl ) GetByIdIncludeDeleted (id int32 ) (* UserModel , error ) {
142
150
var model UserModel
143
151
err := impl .dbConnection .Model (& model ).Where ("id = ?" , id ).Select ()
152
+ model .EmailId = util .ConvertEmailToLowerCase (model .EmailId )
144
153
return & model , err
145
154
}
146
155
@@ -150,6 +159,9 @@ func (impl UserRepositoryImpl) GetAllExcludingApiTokenUser() ([]UserModel, error
150
159
Where ("active = ?" , true ).
151
160
Where ("user_type is NULL or user_type != ?" , bean .USER_TYPE_API_TOKEN ).
152
161
Order ("updated_on desc" ).Select ()
162
+ for i , user := range userModel {
163
+ userModel [i ].EmailId = util .ConvertEmailToLowerCase (user .EmailId )
164
+ }
153
165
return userModel , err
154
166
}
155
167
@@ -160,20 +172,23 @@ func (impl UserRepositoryImpl) GetAllExecutingQuery(query string) ([]UserModel,
160
172
impl .Logger .Error ("error in GetAllExecutingQuery" , "err" , err , "query" , query )
161
173
return nil , err
162
174
}
175
+ for i , user := range userModel {
176
+ userModel [i ].EmailId = util .ConvertEmailToLowerCase (user .EmailId )
177
+ }
163
178
return userModel , err
164
179
}
165
180
166
181
func (impl UserRepositoryImpl ) FetchActiveUserByEmail (email string ) (bean.UserInfo , error ) {
167
182
var users bean.UserInfo
168
183
169
- query := "SELECT u.id, u.email_id, u.access_token, u.user_type FROM users u " +
170
- "WHERE u.active = true and u.email_id ILIKE ? order by u.updated_on desc"
184
+ query := fmt . Sprintf ( "SELECT u.id, u.email_id, u.access_token, u.user_type FROM users u" +
185
+ " WHERE u.active = true and %s order by u.updated_on desc" , helper . GetEmailSearchQuery ( "u" , email ))
171
186
_ , err := impl .dbConnection .Query (& users , query , email )
172
187
if err != nil {
173
- impl .Logger .Error ("Exception caught:" , err )
188
+ impl .Logger .Errorw ("Exception caught:" , "err " , err )
174
189
return users , err
175
190
}
176
-
191
+ users . EmailId = util . ConvertEmailToLowerCase ( email )
177
192
return users , nil
178
193
}
179
194
@@ -182,11 +197,11 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
182
197
var users []bean.UserRole
183
198
var userFinal bean.UserInfo
184
199
185
- query := "SELECT u.id, u.email_id, u.user_type, r.role FROM users u" +
186
- " INNER JOIN user_roles ur ON ur.user_id=u.id" +
187
- " INNER JOIN roles r ON r.id=ur.role_id" +
188
- " WHERE u.email_id= ? and u.active = true" +
189
- " ORDER BY u.updated_on desc;"
200
+ query := fmt . Sprintf ( "SELECT u.id, u.email_id, u.user_type, r.role FROM users u" +
201
+ " INNER JOIN user_roles ur ON ur.user_id=u.id" +
202
+ " INNER JOIN roles r ON r.id=ur.role_id" +
203
+ " WHERE %s and u.active = true" +
204
+ " ORDER BY u.updated_on desc;" , helper . GetEmailSearchQuery ( "u" , email ))
190
205
_ , err := impl .dbConnection .Query (& users , query , email )
191
206
if err != nil {
192
207
return userFinal , err
@@ -196,7 +211,7 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
196
211
for _ , item := range users {
197
212
userFinal .Exist = true
198
213
userFinal .Id = item .Id
199
- userFinal .EmailId = item .EmailId
214
+ userFinal .EmailId = util . ConvertEmailToLowerCase ( item .EmailId )
200
215
role = append (role , item .Role )
201
216
}
202
217
userFinal .Roles = role
@@ -205,6 +220,9 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
205
220
func (impl UserRepositoryImpl ) GetByIds (ids []int32 ) ([]UserModel , error ) {
206
221
var model []UserModel
207
222
err := impl .dbConnection .Model (& model ).Where ("id in (?)" , pg .In (ids )).Where ("active = ?" , true ).Select ()
223
+ for i , m := range model {
224
+ model [i ].EmailId = util .ConvertEmailToLowerCase (m .EmailId )
225
+ }
208
226
return model , err
209
227
}
210
228
@@ -215,15 +233,19 @@ func (impl *UserRepositoryImpl) GetConnection() (dbConnection *pg.DB) {
215
233
func (impl UserRepositoryImpl ) FetchUserMatchesByEmailIdExcludingApiTokenUser (email string ) ([]UserModel , error ) {
216
234
var model []UserModel
217
235
err := impl .dbConnection .Model (& model ).
218
- Where ("email_id like (?)" , "%" + email + "%" ).
236
+ Where ("email_id ilike (?)" , "%" + email + "%" ).
219
237
Where ("user_type is NULL or user_type != ?" , bean .USER_TYPE_API_TOKEN ).
220
238
Where ("active = ?" , true ).Select ()
239
+ for i , m := range model {
240
+ model [i ].EmailId = util .ConvertEmailToLowerCase (m .EmailId )
241
+ }
221
242
return model , err
222
243
}
223
244
224
245
func (impl UserRepositoryImpl ) FetchActiveOrDeletedUserByEmail (email string ) (* UserModel , error ) {
225
246
var model UserModel
226
247
err := impl .dbConnection .Model (& model ).Where ("email_id ILIKE (?)" , email ).Limit (1 ).Select ()
248
+ model .EmailId = util .ConvertEmailToLowerCase (email )
227
249
return & model , err
228
250
}
229
251
0 commit comments