2020package repository
2121
2222import (
23+ "fmt"
2324 "github.com/devtron-labs/devtron/api/bean"
2425 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"
2528 "github.com/devtron-labs/devtron/pkg/sql"
2629 "github.com/go-pg/pg"
2730 "go.uber.org/zap"
@@ -59,13 +62,14 @@ func NewUserRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *User
5962}
6063
6164type 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:"-"`
6973 sql.AuditLog
7074}
7175
@@ -79,6 +83,8 @@ type UserRoleModel struct {
7983}
8084
8185func (impl UserRepositoryImpl ) CreateUser (userModel * UserModel , tx * pg.Tx ) (* UserModel , error ) {
86+ userModel .RequestEmailId = userModel .EmailId
87+ userModel .EmailId = util .ConvertEmailToLowerCase (userModel .EmailId )
8288 err := tx .Insert (userModel )
8389 if err != nil {
8490 impl .Logger .Error (err )
@@ -88,6 +94,7 @@ func (impl UserRepositoryImpl) CreateUser(userModel *UserModel, tx *pg.Tx) (*Use
8894 return userModel , nil
8995}
9096func (impl UserRepositoryImpl ) UpdateUser (userModel * UserModel , tx * pg.Tx ) (* UserModel , error ) {
97+ userModel .EmailId = util .ConvertEmailToLowerCase (userModel .EmailId )
9198 err := tx .Update (userModel )
9299 if err != nil {
93100 impl .Logger .Error (err )
@@ -117,6 +124,7 @@ func (impl UserRepositoryImpl) UpdateToInactiveByIds(ids []int32, tx *pg.Tx, log
117124func (impl UserRepositoryImpl ) GetById (id int32 ) (* UserModel , error ) {
118125 var model UserModel
119126 err := impl .dbConnection .Model (& model ).Where ("id = ?" , id ).Where ("active = ?" , true ).Select ()
127+ model .EmailId = util .ConvertEmailToLowerCase (model .EmailId )
120128 return & model , err
121129}
122130
@@ -134,13 +142,14 @@ func (impl UserRepositoryImpl) GetEmailByIds(ids []int32) ([]string, error) {
134142 for _ , model := range models {
135143 userEmails = append (userEmails , model .EmailId )
136144 }
137- return userEmails , err
145+ return util . ConvertEmailsToLowerCase ( userEmails ) , err
138146
139147}
140148
141149func (impl UserRepositoryImpl ) GetByIdIncludeDeleted (id int32 ) (* UserModel , error ) {
142150 var model UserModel
143151 err := impl .dbConnection .Model (& model ).Where ("id = ?" , id ).Select ()
152+ model .EmailId = util .ConvertEmailToLowerCase (model .EmailId )
144153 return & model , err
145154}
146155
@@ -150,6 +159,9 @@ func (impl UserRepositoryImpl) GetAllExcludingApiTokenUser() ([]UserModel, error
150159 Where ("active = ?" , true ).
151160 Where ("user_type is NULL or user_type != ?" , bean .USER_TYPE_API_TOKEN ).
152161 Order ("updated_on desc" ).Select ()
162+ for i , user := range userModel {
163+ userModel [i ].EmailId = util .ConvertEmailToLowerCase (user .EmailId )
164+ }
153165 return userModel , err
154166}
155167
@@ -160,20 +172,23 @@ func (impl UserRepositoryImpl) GetAllExecutingQuery(query string) ([]UserModel,
160172 impl .Logger .Error ("error in GetAllExecutingQuery" , "err" , err , "query" , query )
161173 return nil , err
162174 }
175+ for i , user := range userModel {
176+ userModel [i ].EmailId = util .ConvertEmailToLowerCase (user .EmailId )
177+ }
163178 return userModel , err
164179}
165180
166181func (impl UserRepositoryImpl ) FetchActiveUserByEmail (email string ) (bean.UserInfo , error ) {
167182 var users bean.UserInfo
168183
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 ))
171186 _ , err := impl .dbConnection .Query (& users , query , email )
172187 if err != nil {
173- impl .Logger .Error ("Exception caught:" , err )
188+ impl .Logger .Errorw ("Exception caught:" , "err " , err )
174189 return users , err
175190 }
176-
191+ users . EmailId = util . ConvertEmailToLowerCase ( email )
177192 return users , nil
178193}
179194
@@ -182,11 +197,11 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
182197 var users []bean.UserRole
183198 var userFinal bean.UserInfo
184199
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 ))
190205 _ , err := impl .dbConnection .Query (& users , query , email )
191206 if err != nil {
192207 return userFinal , err
@@ -196,7 +211,7 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
196211 for _ , item := range users {
197212 userFinal .Exist = true
198213 userFinal .Id = item .Id
199- userFinal .EmailId = item .EmailId
214+ userFinal .EmailId = util . ConvertEmailToLowerCase ( item .EmailId )
200215 role = append (role , item .Role )
201216 }
202217 userFinal .Roles = role
@@ -205,6 +220,9 @@ func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserIn
205220func (impl UserRepositoryImpl ) GetByIds (ids []int32 ) ([]UserModel , error ) {
206221 var model []UserModel
207222 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+ }
208226 return model , err
209227}
210228
@@ -215,15 +233,19 @@ func (impl *UserRepositoryImpl) GetConnection() (dbConnection *pg.DB) {
215233func (impl UserRepositoryImpl ) FetchUserMatchesByEmailIdExcludingApiTokenUser (email string ) ([]UserModel , error ) {
216234 var model []UserModel
217235 err := impl .dbConnection .Model (& model ).
218- Where ("email_id like (?)" , "%" + email + "%" ).
236+ Where ("email_id ilike (?)" , "%" + email + "%" ).
219237 Where ("user_type is NULL or user_type != ?" , bean .USER_TYPE_API_TOKEN ).
220238 Where ("active = ?" , true ).Select ()
239+ for i , m := range model {
240+ model [i ].EmailId = util .ConvertEmailToLowerCase (m .EmailId )
241+ }
221242 return model , err
222243}
223244
224245func (impl UserRepositoryImpl ) FetchActiveOrDeletedUserByEmail (email string ) (* UserModel , error ) {
225246 var model UserModel
226247 err := impl .dbConnection .Model (& model ).Where ("email_id ILIKE (?)" , email ).Limit (1 ).Select ()
248+ model .EmailId = util .ConvertEmailToLowerCase (email )
227249 return & model , err
228250}
229251
0 commit comments