From dfc6db737411bd4ad68a803be5182f06055a1769 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 10 Apr 2017 08:19:49 -0400 Subject: Refactor switching login type code into app layer and add v4 endpoint (#6000) * Refactor switching login type code into app layer and add v4 endpoint * Fix unit test --- model/client4.go | 10 +++++++ model/switch_request.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ model/switch_request_test.go | 19 ++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 model/switch_request.go create mode 100644 model/switch_request_test.go (limited to 'model') diff --git a/model/client4.go b/model/client4.go index 634c477e2..6d4772431 100644 --- a/model/client4.go +++ b/model/client4.go @@ -409,6 +409,16 @@ func (c *Client4) Logout() (bool, *Response) { } } +// SwitchAccountType changes a user's login type from one type to another. +func (c *Client4) SwitchAccountType(switchRequest *SwitchRequest) (string, *Response) { + if r, err := c.DoApiPost(c.GetUsersRoute()+"/login/switch", switchRequest.ToJson()); err != nil { + return "", &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return MapFromJson(r.Body)["follow_link"], BuildResponse(r) + } +} + // User Section // CreateUser creates a user in the system based on the provided user struct. diff --git a/model/switch_request.go b/model/switch_request.go new file mode 100644 index 000000000..44fbb27a9 --- /dev/null +++ b/model/switch_request.go @@ -0,0 +1,62 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +type SwitchRequest struct { + CurrentService string `json:"current_service"` + NewService string `json:"new_service"` + Email string `json:"email"` + Password string `json:"current_password"` + NewPassword string `json:"new_password"` + MfaCode string `json:"mfa_code"` + LdapId string `json:"ldap_id"` +} + +func (o *SwitchRequest) ToJson() string { + b, err := json.Marshal(o) + if err != nil { + return "" + } else { + return string(b) + } +} + +func SwitchRequestFromJson(data io.Reader) *SwitchRequest { + decoder := json.NewDecoder(data) + var o SwitchRequest + err := decoder.Decode(&o) + if err == nil { + return &o + } else { + return nil + } +} + +func (o *SwitchRequest) EmailToOAuth() bool { + return o.CurrentService == USER_AUTH_SERVICE_EMAIL && + (o.NewService == USER_AUTH_SERVICE_SAML || + o.NewService == USER_AUTH_SERVICE_GITLAB || + o.NewService == SERVICE_GOOGLE || + o.NewService == SERVICE_OFFICE365) +} + +func (o *SwitchRequest) OAuthToEmail() bool { + return (o.CurrentService == USER_AUTH_SERVICE_SAML || + o.CurrentService == USER_AUTH_SERVICE_GITLAB || + o.CurrentService == SERVICE_GOOGLE || + o.CurrentService == SERVICE_OFFICE365) && o.NewService == USER_AUTH_SERVICE_EMAIL +} + +func (o *SwitchRequest) EmailToLdap() bool { + return o.CurrentService == USER_AUTH_SERVICE_EMAIL && o.NewService == USER_AUTH_SERVICE_LDAP +} + +func (o *SwitchRequest) LdapToEmail() bool { + return o.CurrentService == USER_AUTH_SERVICE_LDAP && o.NewService == USER_AUTH_SERVICE_EMAIL +} diff --git a/model/switch_request_test.go b/model/switch_request_test.go new file mode 100644 index 000000000..b85846f78 --- /dev/null +++ b/model/switch_request_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "strings" + "testing" +) + +func TestSwitchRequestJson(t *testing.T) { + o := SwitchRequest{Email: NewId(), Password: NewId()} + json := o.ToJson() + ro := SwitchRequestFromJson(strings.NewReader(json)) + + if o.Email != ro.Email { + t.Fatal("Emails do not match") + } +} -- cgit v1.2.3-1-g7c22