From 216146a0fcb3593f4d96dd360bad4d47eddb3b71 Mon Sep 17 00:00:00 2001 From: Alexander Giesa Date: Wed, 12 Sep 2018 18:42:14 +0200 Subject: [PATCH 1/4] fix 'Unknow column...' error #12 --- providers/password/handlers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/providers/password/handlers.go b/providers/password/handlers.go index a74ceb7..472316c 100644 --- a/providers/password/handlers.go +++ b/providers/password/handlers.go @@ -24,7 +24,8 @@ var DefaultAuthorizeHandler = func(context *auth.Context) (*claims.Claims, error authInfo.Provider = provider.GetName() authInfo.UID = strings.TrimSpace(req.Form.Get("login")) - if tx.Model(context.Auth.AuthIdentityModel).Where(authInfo).Scan(&authInfo).RecordNotFound() { + authwhere := auth_identity.AuthIdentity{Basic: authInfo} + if tx.Model(context.Auth.AuthIdentityModel).Where(authwhere).Scan(&authInfo).RecordNotFound() { return nil, auth.ErrInvalidAccount } From 574194854737d9a5ccae67f7737058354758d7ce Mon Sep 17 00:00:00 2001 From: Alexander Giesa Date: Sun, 30 Sep 2018 12:36:41 +0200 Subject: [PATCH 2/4] fix password provider signup and mail confirm issue Signed-off-by: Alexander Giesa --- providers/password/confirm.go | 5 ++++- providers/password/handlers.go | 17 +++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/providers/password/confirm.go b/providers/password/confirm.go index 72ff442..2600199 100644 --- a/providers/password/confirm.go +++ b/providers/password/confirm.go @@ -76,7 +76,10 @@ var DefaultConfirmHandler = func(context *auth.Context) error { if err = claims.Valid(); err == nil { authInfo.Provider = provider.GetName() authInfo.UID = claims.Id + authInfo.UserID = claims.UserID authIdentity := reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface() + authwhere := auth_identity.AuthIdentity{Basic: authInfo} + authwhere.Basic = authInfo if tx.Where(authInfo).First(authIdentity).RecordNotFound() { err = auth.ErrInvalidAccount @@ -86,7 +89,7 @@ var DefaultConfirmHandler = func(context *auth.Context) error { if authInfo.ConfirmedAt == nil { now := time.Now() authInfo.ConfirmedAt = &now - if err = tx.Model(authIdentity).Update(authInfo).Error; err == nil { + if err = tx.Model(authwhere).Where("user_id = ?", authInfo.UserID).Update(authInfo).Error; err == nil { context.SessionStorer.Flash(context.Writer, context.Request, session.Message{Message: ConfirmedAccountFlashMessage, Type: "success"}) context.Auth.Redirector.Redirect(context.Writer, context.Request, "confirm") return nil diff --git a/providers/password/handlers.go b/providers/password/handlers.go index 472316c..f16e64c 100644 --- a/providers/password/handlers.go +++ b/providers/password/handlers.go @@ -1,13 +1,11 @@ package password import ( - "reflect" "strings" "github.com/qor/auth" "github.com/qor/auth/auth_identity" "github.com/qor/auth/claims" - "github.com/qor/qor/utils" "github.com/qor/session" ) @@ -23,9 +21,9 @@ var DefaultAuthorizeHandler = func(context *auth.Context) (*claims.Claims, error req.ParseForm() authInfo.Provider = provider.GetName() authInfo.UID = strings.TrimSpace(req.Form.Get("login")) - authwhere := auth_identity.AuthIdentity{Basic: authInfo} - if tx.Model(context.Auth.AuthIdentityModel).Where(authwhere).Scan(&authInfo).RecordNotFound() { + + if tx.Model(context.Auth.AuthIdentityModel).Where(authwhere).Scan(&authInfo).RecordNotFound() { //authInfo in authwhere geƤndert return nil, auth.ErrInvalidAccount } @@ -66,8 +64,10 @@ var DefaultRegisterHandler = func(context *auth.Context) (*claims.Claims, error) authInfo.Provider = provider.GetName() authInfo.UID = strings.TrimSpace(req.Form.Get("login")) + authwhere := auth_identity.AuthIdentity{Basic: authInfo} + //authIdentity := reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface() - if !tx.Model(context.Auth.AuthIdentityModel).Where(authInfo).Scan(&authInfo).RecordNotFound() { + if !tx.Model(context.Auth.AuthIdentityModel).Where(authwhere).Scan(&authInfo).RecordNotFound() { return nil, auth.ErrInvalidAccount } @@ -82,9 +82,10 @@ var DefaultRegisterHandler = func(context *auth.Context) (*claims.Claims, error) return nil, err } - // create auth identity - authIdentity := reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface() - if err = tx.Where(authInfo).FirstOrCreate(authIdentity).Error; err == nil { + // copy authInfo to authwhere because it has no login credencials + authwhere.Basic = authInfo + // store login credencials + if err = tx.Where(authwhere).FirstOrCreate(&authwhere).Error; err == nil { if provider.Config.Confirmable { context.SessionStorer.Flash(context.Writer, req, session.Message{Message: ConfirmFlashMessage, Type: "success"}) err = provider.Config.ConfirmMailer(schema.Email, context, authInfo.ToClaims(), currentUser) From fda1f0487641f21bcc64d9375738d97ecbbab6f8 Mon Sep 17 00:00:00 2001 From: Alexander Giesa Date: Wed, 26 Dec 2018 00:29:43 +0100 Subject: [PATCH 3/4] fix comfirmation token/confirm issue --- providers/password/confirm.go | 14 +++++++++++--- .../views/mailers/auth/confirmation.text.tmpl | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 providers/password/views/mailers/auth/confirmation.text.tmpl diff --git a/providers/password/confirm.go b/providers/password/confirm.go index 2600199..aedc875 100644 --- a/providers/password/confirm.go +++ b/providers/password/confirm.go @@ -40,6 +40,7 @@ var DefaultConfirmationMailer = func(email string, context *auth.Context, claims return context.Auth.Mailer.Send( mailer.Email{ TO: []mail.Address{{Address: email}}, + From: &mail.Address{Address: "info@77g.de"}, Subject: ConfirmationMailSubject, }, mailer.Template{ Name: "auth/confirmation", @@ -68,6 +69,7 @@ var DefaultConfirmHandler = func(context *auth.Context) error { provider, _ = context.Provider.(*Provider) tx = context.Auth.GetDB(context.Request) token = context.Request.URL.Query().Get("token") + currentUser = reflect.New(utils.ModelType(context.Auth.Config.UserModel)).Interface() ) claims, err := context.SessionStorer.ValidateClaims(token) @@ -79,16 +81,22 @@ var DefaultConfirmHandler = func(context *auth.Context) error { authInfo.UserID = claims.UserID authIdentity := reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface() authwhere := auth_identity.AuthIdentity{Basic: authInfo} - authwhere.Basic = authInfo - if tx.Where(authInfo).First(authIdentity).RecordNotFound() { + if tx.Where(authwhere).First(authIdentity).RecordNotFound() { err = auth.ErrInvalidAccount + return err } + //load user to get ConfirmedAt date + tx.Where(&authwhere).First(&authwhere) if err == nil { - if authInfo.ConfirmedAt == nil { + if authwhere.Basic.ConfirmedAt == nil { now := time.Now() authInfo.ConfirmedAt = &now + + //User updaten + tx.Model(¤tUser).Where("ID = ? and email = ?", authwhere.Basic.UserID, authwhere.Basic.UID).Updates(map[string]interface{}{"confirm_token": token, "confirmed": true}) + if err = tx.Model(authwhere).Where("user_id = ?", authInfo.UserID).Update(authInfo).Error; err == nil { context.SessionStorer.Flash(context.Writer, context.Request, session.Message{Message: ConfirmedAccountFlashMessage, Type: "success"}) context.Auth.Redirector.Redirect(context.Writer, context.Request, "confirm") diff --git a/providers/password/views/mailers/auth/confirmation.text.tmpl b/providers/password/views/mailers/auth/confirmation.text.tmpl new file mode 100644 index 0000000..c35110c --- /dev/null +++ b/providers/password/views/mailers/auth/confirmation.text.tmpl @@ -0,0 +1,3 @@ +

Please click on the below link to validate your email address:

+ +

{{confirm_url}}

From f99caf71903a2f155f25f07a3563ea567860043f Mon Sep 17 00:00:00 2001 From: Alexander Giesa Date: Wed, 2 Jan 2019 16:23:23 +0100 Subject: [PATCH 4/4] added customer as default role --- providers/password/confirm.go | 2 +- schema.go | 1 + user_storer.go | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/providers/password/confirm.go b/providers/password/confirm.go index aedc875..3da6e33 100644 --- a/providers/password/confirm.go +++ b/providers/password/confirm.go @@ -94,7 +94,7 @@ var DefaultConfirmHandler = func(context *auth.Context) error { now := time.Now() authInfo.ConfirmedAt = &now - //User updaten + //add token to user table tx.Model(¤tUser).Where("ID = ? and email = ?", authwhere.Basic.UserID, authwhere.Basic.UID).Updates(map[string]interface{}{"confirm_token": token, "confirmed": true}) if err = tx.Model(authwhere).Where("user_id = ?", authInfo.UserID).Update(authInfo).Error; err == nil { diff --git a/schema.go b/schema.go index 5638c18..fe3940c 100644 --- a/schema.go +++ b/schema.go @@ -13,6 +13,7 @@ type Schema struct { Image string Phone string URL string + Role string `gorm:"default:'Costumer'"` RawInfo interface{} } diff --git a/user_storer.go b/user_storer.go index fb35b27..9787f3d 100644 --- a/user_storer.go +++ b/user_storer.go @@ -67,6 +67,7 @@ func (UserStorer) Save(schema *Schema, context *Context) (user interface{}, user if context.Auth.Config.UserModel != nil { currentUser := reflect.New(utils.ModelType(context.Auth.Config.UserModel)).Interface() + schema.Role = "Costumer" //set the default role for every new user copier.Copy(currentUser, schema) err = tx.Create(currentUser).Error return currentUser, fmt.Sprint(tx.NewScope(currentUser).PrimaryKeyValue()), err