Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding dashboard field to user data type #335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand All @@ -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,
}
}

Expand Down
51 changes: 51 additions & 0 deletions library/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package library

import (
"fmt"
"strconv"

"github.com/go-vela/types/constants"
)
Expand All @@ -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.
Expand All @@ -38,6 +40,7 @@ func (u *User) Sanitize() *User {
Favorites: u.Favorites,
Active: u.Active,
Admin: u.Admin,
Dashboards: u.Dashboards,
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -260,18 +276,53 @@ 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,
Token: %s,
}`,
u.GetActive(),
u.GetAdmin(),
u.GetDashboards(),
u.GetFavorites(),
u.GetID(),
u.GetName(),
Expand Down
2 changes: 2 additions & 0 deletions library/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ func TestLibrary_User_String(t *testing.T) {
want := fmt.Sprintf(`{
Active: %t,
Admin: %t,
Dashboards: %s,
Favorites: %s,
ID: %d,
Name: %s,
Token: %s,
}`,
u.GetActive(),
u.GetAdmin(),
u.GetDashboards(),
u.GetFavorites(),
u.GetID(),
u.GetName(),
Expand Down