diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/client.go | 2 | ||||
-rw-r--r-- | model/config.go | 43 | ||||
-rw-r--r-- | model/file_info.go | 72 | ||||
-rw-r--r-- | model/file_info_test.go | 76 | ||||
-rw-r--r-- | model/gitlab/gitlab.go (renamed from model/gitlab.go) | 34 | ||||
-rw-r--r-- | model/version.go | 1 |
6 files changed, 220 insertions, 8 deletions
diff --git a/model/client.go b/model/client.go index d3f76817d..b9b97dedc 100644 --- a/model/client.go +++ b/model/client.go @@ -717,7 +717,7 @@ func (c *Client) GetFileInfo(url string) (*Result, *AppError) { return nil, AppErrorFromJson(rp.Body) } else { return &Result{rp.Header.Get(HEADER_REQUEST_ID), - rp.Header.Get(HEADER_ETAG_SERVER), MapFromJson(rp.Body)}, nil + rp.Header.Get(HEADER_ETAG_SERVER), FileInfoFromJson(rp.Body)}, nil } } diff --git a/model/config.go b/model/config.go index 06cb9829e..38ef81a85 100644 --- a/model/config.go +++ b/model/config.go @@ -20,6 +20,7 @@ const ( DATABASE_DRIVER_POSTGRES = "postgres" SERVICE_GITLAB = "gitlab" + SERVICE_GOOGLE = "google" ) type ServiceSettings struct { @@ -133,6 +134,26 @@ type TeamSettings struct { EnableTeamListing *bool } +type LdapSettings struct { + // Basic + Enable *bool + LdapServer *string + LdapPort *int + BaseDN *string + BindUsername *string + BindPassword *string + + // User Mapping + FirstNameAttribute *string + LastNameAttribute *string + EmailAttribute *string + UsernameAttribute *string + IdAttribute *string + + // Advansed + QueryTimeout *int +} + type Config struct { ServiceSettings ServiceSettings TeamSettings TeamSettings @@ -144,6 +165,8 @@ type Config struct { PrivacySettings PrivacySettings SupportSettings SupportSettings GitLabSettings SSOSettings + GoogleSettings SSOSettings + LdapSettings LdapSettings } func (o *Config) ToJson() string { @@ -156,8 +179,11 @@ func (o *Config) ToJson() string { } func (o *Config) GetSSOService(service string) *SSOSettings { - if service == SERVICE_GITLAB { + switch service { + case SERVICE_GITLAB: return &o.GitLabSettings + case SERVICE_GOOGLE: + return &o.GoogleSettings } return nil @@ -251,6 +277,21 @@ func (o *Config) SetDefaults() { o.SupportSettings.SupportEmail = new(string) *o.SupportSettings.SupportEmail = "feedback@mattermost.com" } + + if o.LdapSettings.LdapPort == nil { + o.LdapSettings.LdapPort = new(int) + *o.LdapSettings.LdapPort = 389 + } + + if o.LdapSettings.QueryTimeout == nil { + o.LdapSettings.QueryTimeout = new(int) + *o.LdapSettings.QueryTimeout = 60 + } + + if o.LdapSettings.Enable == nil { + o.LdapSettings.Enable = new(bool) + *o.LdapSettings.Enable = false + } } func (o *Config) IsValid() *AppError { diff --git a/model/file_info.go b/model/file_info.go new file mode 100644 index 000000000..741b4e55d --- /dev/null +++ b/model/file_info.go @@ -0,0 +1,72 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "bytes" + "encoding/json" + "image/gif" + "io" + "mime" + "path/filepath" +) + +type FileInfo struct { + Filename string `json:"filename"` + Size int `json:"size"` + Extension string `json:"extension"` + MimeType string `json:"mime_type"` + HasPreviewImage bool `json:"has_preview_image"` +} + +func GetInfoForBytes(filename string, data []byte) (*FileInfo, *AppError) { + size := len(data) + + var mimeType string + extension := filepath.Ext(filename) + isImage := IsFileExtImage(extension) + if isImage { + mimeType = GetImageMimeType(extension) + } else { + mimeType = mime.TypeByExtension(extension) + } + + hasPreviewImage := isImage + if mimeType == "image/gif" { + // just show the gif itself instead of a preview image for animated gifs + if gifImage, err := gif.DecodeAll(bytes.NewReader(data)); err != nil { + return nil, NewAppError("GetInfoForBytes", "Could not decode gif.", "filename="+filename) + } else { + hasPreviewImage = len(gifImage.Image) == 1 + } + } + + return &FileInfo{ + Filename: filename, + Size: size, + Extension: extension[1:], + MimeType: mimeType, + HasPreviewImage: hasPreviewImage, + }, nil +} + +func (info *FileInfo) ToJson() string { + b, err := json.Marshal(info) + if err != nil { + return "" + } else { + return string(b) + } +} + +func FileInfoFromJson(data io.Reader) *FileInfo { + decoder := json.NewDecoder(data) + + var info FileInfo + if err := decoder.Decode(&info); err != nil { + return nil + } else { + return &info + } +} diff --git a/model/file_info_test.go b/model/file_info_test.go new file mode 100644 index 000000000..ecf0d509c --- /dev/null +++ b/model/file_info_test.go @@ -0,0 +1,76 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/base64" + "io/ioutil" + "testing" +) + +func TestGetInfoForBytes(t *testing.T) { + fakeFile := make([]byte, 1000) + + if info, err := GetInfoForBytes("file.txt", fakeFile); err != nil { + t.Fatal(err) + } else if info.Filename != "file.txt" { + t.Fatalf("Got incorrect filename: %v", info.Filename) + } else if info.Size != 1000 { + t.Fatalf("Got incorrect size: %v", info.Size) + } else if info.Extension != "txt" { + t.Fatalf("Git incorrect file extension: %v", info.Extension) + } else if info.MimeType != "text/plain; charset=utf-8" { + t.Fatalf("Got incorrect mime type: %v", info.MimeType) + } else if info.HasPreviewImage { + t.Fatalf("Got HasPreviewImage = true for non-image file") + } + + if info, err := GetInfoForBytes("file.png", fakeFile); err != nil { + t.Fatal(err) + } else if info.Filename != "file.png" { + t.Fatalf("Got incorrect filename: %v", info.Filename) + } else if info.Size != 1000 { + t.Fatalf("Got incorrect size: %v", info.Size) + } else if info.Extension != "png" { + t.Fatalf("Git incorrect file extension: %v", info.Extension) + } else if info.MimeType != "image/png" { + t.Fatalf("Got incorrect mime type: %v", info.MimeType) + } else if !info.HasPreviewImage { + t.Fatalf("Got HasPreviewImage = false for image") + } + + // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever + gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=") + if info, err := GetInfoForBytes("handtinywhite.gif", gifFile); err != nil { + t.Fatal(err) + } else if info.Filename != "handtinywhite.gif" { + t.Fatalf("Got incorrect filename: %v", info.Filename) + } else if info.Size != 35 { + t.Fatalf("Got incorrect size: %v", info.Size) + } else if info.Extension != "gif" { + t.Fatalf("Git incorrect file extension: %v", info.Extension) + } else if info.MimeType != "image/gif" { + t.Fatalf("Got incorrect mime type: %v", info.MimeType) + } else if !info.HasPreviewImage { + t.Fatalf("Got HasPreviewImage = false for static gif") + } + + animatedGifFile, err := ioutil.ReadFile("../web/static/images/testgif.gif") + if err != nil { + t.Fatalf("Failed to load testgif.gif: %v", err.Error()) + } + if info, err := GetInfoForBytes("testgif.gif", animatedGifFile); err != nil { + t.Fatal(err) + } else if info.Filename != "testgif.gif" { + t.Fatalf("Got incorrect filename: %v", info.Filename) + } else if info.Size != 38689 { + t.Fatalf("Got incorrect size: %v", info.Size) + } else if info.Extension != "gif" { + t.Fatalf("Git incorrect file extension: %v", info.Extension) + } else if info.MimeType != "image/gif" { + t.Fatalf("Got incorrect mime type: %v", info.MimeType) + } else if info.HasPreviewImage { + t.Fatalf("Got HasPreviewImage = true for animated gif") + } +} diff --git a/model/gitlab.go b/model/gitlab/gitlab.go index 2a8756807..8b96c64f6 100644 --- a/model/gitlab.go +++ b/model/gitlab/gitlab.go @@ -1,10 +1,12 @@ // Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. -package model +package oauthgitlab import ( "encoding/json" + "github.com/mattermost/platform/einterfaces" + "github.com/mattermost/platform/model" "io" "strconv" "strings" @@ -14,6 +16,9 @@ const ( USER_AUTH_SERVICE_GITLAB = "gitlab" ) +type GitLabProvider struct { +} + type GitLabUser struct { Id int64 `json:"id"` Username string `json:"username"` @@ -22,13 +27,18 @@ type GitLabUser struct { Name string `json:"name"` } -func UserFromGitLabUser(glu *GitLabUser) *User { - user := &User{} +func init() { + provider := &GitLabProvider{} + einterfaces.RegisterOauthProvider(USER_AUTH_SERVICE_GITLAB, provider) +} + +func userFromGitLabUser(glu *GitLabUser) *model.User { + user := &model.User{} username := glu.Username if username == "" { username = glu.Login } - user.Username = CleanUsername(username) + user.Username = model.CleanUsername(username) splitName := strings.Split(glu.Name, " ") if len(splitName) == 2 { user.FirstName = splitName[0] @@ -46,7 +56,7 @@ func UserFromGitLabUser(glu *GitLabUser) *User { return user } -func GitLabUserFromJson(data io.Reader) *GitLabUser { +func gitLabUserFromJson(data io.Reader) *GitLabUser { decoder := json.NewDecoder(data) var glu GitLabUser err := decoder.Decode(&glu) @@ -57,6 +67,18 @@ func GitLabUserFromJson(data io.Reader) *GitLabUser { } } -func (glu *GitLabUser) GetAuthData() string { +func (glu *GitLabUser) getAuthData() string { return strconv.FormatInt(glu.Id, 10) } + +func (m *GitLabProvider) GetIdentifier() string { + return USER_AUTH_SERVICE_GITLAB +} + +func (m *GitLabProvider) GetUserFromJson(data io.Reader) *model.User { + return userFromGitLabUser(gitLabUserFromJson(data)) +} + +func (m *GitLabProvider) GetAuthDataFromJson(data io.Reader) string { + return gitLabUserFromJson(data).getAuthData() +} diff --git a/model/version.go b/model/version.go index 5e41a28d1..142ddb371 100644 --- a/model/version.go +++ b/model/version.go @@ -27,6 +27,7 @@ var CurrentVersion string = versions[0] var BuildNumber = "_BUILD_NUMBER_" var BuildDate = "_BUILD_DATE_" var BuildHash = "_BUILD_HASH_" +var BuildEnterpriseReady = "_BUILD_ENTERPRISE_READY_" func SplitVersion(version string) (int64, int64, int64) { parts := strings.Split(version, ".") |