diff --git a/database/user.go b/database/user.go index 1e312755..6e3f9e46 100644 --- a/database/user.go +++ b/database/user.go @@ -49,6 +49,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" gorm:"type:varchar(5000)"` } // Decrypt will manipulate the existing user tokens by @@ -210,6 +211,7 @@ func (u *User) ToLibrary() *library.User { user.SetActive(u.Active.Bool) user.SetAdmin(u.Admin.Bool) user.SetFavorites(u.Favorites) + user.SetDashboards(u.Dashboards) return user } @@ -271,6 +273,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: pq.StringArray(u.GetDashboards()), } return user.Nullify() diff --git a/database/user_test.go b/database/user_test.go index fa3ae5d5..de91c4ec 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,6 +158,7 @@ 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() @@ -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..2c95c4ca 100644 --- a/library/user.go +++ b/library/user.go @@ -4,6 +4,7 @@ package library import ( "fmt" + "strconv" "github.com/go-vela/types/constants" ) @@ -20,6 +21,7 @@ type User struct { Favorites *[]string `json:"favorites,omitempty"` Active *bool `json:"active,omitempty"` Admin *bool `json:"admin,omitempty"` + Dashboards *[]string `json:"dashboards,omitempty"` } // Sanitize creates a duplicate of the User without the token values. @@ -38,6 +40,7 @@ func (u *User) Sanitize() *User { Favorites: u.Favorites, Active: u.Active, Admin: u.Admin, + Dashboards: u.Dashboards, } } @@ -156,6 +159,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() []string { + // return zero value if User type or Favorites field is nil + if u == nil || u.Dashboards == nil { + return []string{} + } + + return *u.Dashboards +} + // SetID sets the ID field. // // When the provided User type is nil, it @@ -260,11 +276,45 @@ 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 []string) { + // 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() { + oldID, _ := strconv.ParseInt(dashboard, 10, 64) + if oldID == 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: %s, Favorites: %s, ID: %d, Name: %s, @@ -272,6 +322,7 @@ func (u *User) String() string { }`, u.GetActive(), u.GetAdmin(), + u.GetDashboards(), u.GetFavorites(), u.GetID(), u.GetName(), diff --git a/library/user_test.go b/library/user_test.go index 76657373..8d0f11d9 100644 --- a/library/user_test.go +++ b/library/user_test.go @@ -167,6 +167,7 @@ func TestLibrary_User_String(t *testing.T) { want := fmt.Sprintf(`{ Active: %t, Admin: %t, + Dashboards: %s, Favorites: %s, ID: %d, Name: %s, @@ -174,6 +175,7 @@ func TestLibrary_User_String(t *testing.T) { }`, u.GetActive(), u.GetAdmin(), + u.GetDashboards(), u.GetFavorites(), u.GetID(), u.GetName(),