diff --git a/database/user.go b/database/user.go index 1e312755..1fb89dba 100644 --- a/database/user.go +++ b/database/user.go @@ -6,6 +6,7 @@ import ( "database/sql" "encoding/base64" "errors" + "fmt" "regexp" "github.com/go-vela/types/constants" @@ -49,6 +50,7 @@ type User struct { Favorites pq.StringArray `sql:"favorites" gorm:"type:varchar(5000)"` Active sql.NullBool `sql:"active"` Admin sql.NullBool `sql:"admin"` + Dashboards pq.StringArray `sql:"dashboards"` } // Decrypt will manipulate the existing user tokens by @@ -199,7 +201,7 @@ func (u *User) Nullify() *User { // ToLibrary converts the User type // to a library User type. -func (u *User) ToLibrary() *library.User { +func (u *User) ToLibrary(dashboards []library.Dashboard) *library.User { user := new(library.User) user.SetID(u.ID.Int64) @@ -210,6 +212,7 @@ func (u *User) ToLibrary() *library.User { user.SetActive(u.Active.Bool) user.SetAdmin(u.Admin.Bool) user.SetFavorites(u.Favorites) + user.SetDashboards(dashboards) return user } @@ -262,6 +265,11 @@ func (u *User) Validate() error { // UserFromLibrary converts the library User type // to a database User type. func UserFromLibrary(u *library.User) *User { + dahsboardIDs := []string{} + for _, dashboard := range u.GetDashboards() { + dahsboardIDs = append(dahsboardIDs, fmt.Sprint(dashboard.GetID())) + } + user := &User{ ID: sql.NullInt64{Int64: u.GetID(), Valid: true}, Name: sql.NullString{String: u.GetName(), Valid: true}, @@ -271,6 +279,7 @@ func UserFromLibrary(u *library.User) *User { Active: sql.NullBool{Bool: u.GetActive(), Valid: true}, Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true}, Favorites: pq.StringArray(u.GetFavorites()), + Dashboards: dahsboardIDs, } return user.Nullify() diff --git a/database/user_test.go b/database/user_test.go index fa3ae5d5..b7b2cc07 100644 --- a/database/user_test.go +++ b/database/user_test.go @@ -114,6 +114,7 @@ func TestDatabase_User_Nullify(t *testing.T) { Hash: sql.NullString{String: "", Valid: false}, Active: sql.NullBool{Bool: false, Valid: false}, Admin: sql.NullBool{Bool: false, Valid: false}, + Dashboards: nil, } // setup tests @@ -157,9 +158,10 @@ func TestDatabase_User_ToLibrary(t *testing.T) { want.SetFavorites([]string{"github/octocat"}) want.SetActive(true) want.SetAdmin(false) + want.SetDashboards(nil) // run test - got := testUser().ToLibrary() + got := testUser().ToLibrary(nil) if !reflect.DeepEqual(got, want) { t.Errorf("ToLibrary is %v, want %v", got, want) @@ -244,6 +246,7 @@ func TestDatabase_UserFromLibrary(t *testing.T) { u.SetFavorites([]string{"github/octocat"}) u.SetActive(true) u.SetAdmin(false) + u.SetDashboards(nil) want := testUser() @@ -267,6 +270,7 @@ func testUser() *User { Favorites: []string{"github/octocat"}, Active: sql.NullBool{Bool: true, Valid: true}, Admin: sql.NullBool{Bool: false, Valid: true}, + Dashboards: nil, } } diff --git a/library/user.go b/library/user.go index 3e2deb8e..5bfd5a64 100644 --- a/library/user.go +++ b/library/user.go @@ -12,14 +12,15 @@ import ( // // swagger:model User type User struct { - ID *int64 `json:"id,omitempty"` - Name *string `json:"name,omitempty"` - RefreshToken *string `json:"-"` - Token *string `json:"-"` - Hash *string `json:"-"` // deprecated - Favorites *[]string `json:"favorites,omitempty"` - Active *bool `json:"active,omitempty"` - Admin *bool `json:"admin,omitempty"` + ID *int64 `json:"id,omitempty"` + Name *string `json:"name,omitempty"` + RefreshToken *string `json:"-"` + Token *string `json:"-"` + Hash *string `json:"-"` // deprecated + Favorites *[]string `json:"favorites,omitempty"` + Active *bool `json:"active,omitempty"` + Admin *bool `json:"admin,omitempty"` + Dashboards *[]Dashboard `json:"dashboards,omitempty"` } // Sanitize creates a duplicate of the User without the token values. @@ -38,6 +39,7 @@ func (u *User) Sanitize() *User { Favorites: u.Favorites, Active: u.Active, Admin: u.Admin, + Dashboards: u.Dashboards, } } @@ -156,6 +158,19 @@ func (u *User) GetFavorites() []string { return *u.Favorites } +// GetDashboards returns the Dashboards field. +// +// When the provided User type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (u *User) GetDashboards() []Dashboard { + // return zero value if User type or Favorites field is nil + if u == nil || u.Dashboards == nil { + return []Dashboard{} + } + + return *u.Dashboards +} + // SetID sets the ID field. // // When the provided User type is nil, it @@ -260,11 +275,44 @@ func (u *User) SetFavorites(v []string) { u.Favorites = &v } +// SetDashboard sets the Dashboard field. +// +// When the provided User type is nil, it +// will set nothing and immediately return. +func (u *User) SetDashboards(v []Dashboard) { + // return if User type is nil + if u == nil { + return + } + + u.Dashboards = &v +} + +// SetDefaultDashboard sets the default Dashboard. +// +// When the provided User type is nil, it +// will set nothing and immediately return. +func (u *User) SetDefaultDashboard(d Dashboard) { + dashboards := *u.Dashboards + dID := d.GetID() + + for a, dashboard := range u.GetDashboards() { + if dashboard.GetID() == dID { + hold := dashboards[0] + dashboards[0] = dashboard + dashboards[a] = hold + } + } + + u.Dashboards = &dashboards +} + // String implements the Stringer interface for the User type. func (u *User) String() string { return fmt.Sprintf(`{ Active: %t, Admin: %t, + Dashboards: %d, Favorites: %s, ID: %d, Name: %s, @@ -272,6 +320,7 @@ func (u *User) String() string { }`, u.GetActive(), u.GetAdmin(), + len(u.GetDashboards()), u.GetFavorites(), u.GetID(), u.GetName(),