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
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
7b77bcf87e
Коммит
dfc6db7374
@@ -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.
|
||||
|
||||
62
model/switch_request.go
Обычный файл
62
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
|
||||
}
|
||||
19
model/switch_request_test.go
Обычный файл
19
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")
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user