Skip to content
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
38 changes: 19 additions & 19 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ func (suite *AuthTestSuite) TestAuth() {
tm := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

// Test Create
CreateUser(suite.ctx, "uuid12", "johndoe", "firstdoe", "lastdoe", "orgdoe", "descdoe", []ProjectRoles{ProjectRoles{Project: "ARGO", Roles: []string{"consumer"}}}, "johndoe@fake.email.foo", "TOK3N", []string{"service_admin"}, tm, "", store)
CreateUser(suite.ctx, "uuid12", "johndoe", "firstdoe", "lastdoe", "orgdoe", "descdoe", []ProjectRoles{ProjectRoles{Project: "ARGO", Roles: []string{"consumer"}}}, "johndoe@fake.email.foo", "TOK3N", []string{"service_admin"}, "", "", tm, "", store)
usrs, _ := FindUsers(suite.ctx, "", "uuid12", "", true, store)
usrJSON, _ := usrs.List[0].ExportJSON()
suite.Equal(expUsrJSON, usrJSON)

// Test Create with empty project list
CreateUser(suite.ctx, "uuid13", "empty-proj", "", "", "", "", []ProjectRoles{{Project: "", Roles: []string{"consumer"}}}, "TOK3N", "johndoe@fake.email.foo", []string{"service_admin"}, tm, "", store)
CreateUser(suite.ctx, "uuid13", "empty-proj", "", "", "", "", []ProjectRoles{{Project: "", Roles: []string{"consumer"}}}, "TOK3N", "johndoe@fake.email.foo", []string{"service_admin"}, "", "", tm, "", store)
usrs2, _ := FindUsers(suite.ctx, "", "uuid13", "", true, store)
expusrs2 := Users{List: []User{{UUID: "uuid13", Projects: []ProjectRoles{}, Name: "empty-proj", Token: "TOK3N", Email: "johndoe@fake.email.foo", ServiceRoles: []string{"service_admin"}, CreatedOn: "2009-11-10T23:00:00Z", ModifiedOn: "2009-11-10T23:00:00Z", CreatedBy: ""}}}
suite.Equal(expusrs2, usrs2)
Expand Down Expand Up @@ -491,18 +491,18 @@ func (suite *AuthTestSuite) TestAuth() {
"created_on": "2009-11-10T23:00:00Z",
"modified_on": "2009-11-10T23:00:00Z"
}`
UpdateUser(suite.ctx, "uuid12", "firstdoe2", "lastdoe2", "orgdoe2", "descdoe2", "johnny_doe", nil, "", []string{"consumer", "producer"}, tm, false, store)
UpdateUser(suite.ctx, "uuid12", "firstdoe2", "lastdoe2", "orgdoe2", "descdoe2", "johnny_doe", nil, "", []string{"consumer", "producer"}, "", "", tm, false, store)
usrUpd, _ := FindUsers(suite.ctx, "", "uuid12", "", true, store)
usrUpdJSON, _ := usrUpd.List[0].ExportJSON()
suite.Equal(expUpdate, usrUpdJSON)

// reflect obj true
usrUpd2, _ := UpdateUser(suite.ctx, "uuid12", "", "", "", "", "johnny_doe", nil, "", []string{"consumer", "producer"}, tm, true, store)
usrUpd2, _ := UpdateUser(suite.ctx, "uuid12", "", "", "", "", "johnny_doe", nil, "", []string{"consumer", "producer"}, "", "", tm, true, store)
usrUpdJSON2, _ := usrUpd2.ExportJSON()
suite.Equal(expUpdate, usrUpdJSON2)

// Test update with empty project
UpdateUser(suite.ctx, "uuid13", "", "", "", "", "empty-proj", []ProjectRoles{{Project: "", Roles: []string{"consumer"}}}, "johndoe@fake.email.foo", []string{"service_admin"}, tm, false, store)
UpdateUser(suite.ctx, "uuid13", "", "", "", "", "empty-proj", []ProjectRoles{{Project: "", Roles: []string{"consumer"}}}, "johndoe@fake.email.foo", []string{"service_admin"}, "", "", tm, false, store)
usrs2, _ = FindUsers(suite.ctx, "", "uuid13", "", true, store)
expusrs2 = Users{List: []User{{UUID: "uuid13", Projects: []ProjectRoles{}, Name: "empty-proj", Token: "TOK3N", Email: "johndoe@fake.email.foo", ServiceRoles: []string{"service_admin"}, CreatedOn: "2009-11-10T23:00:00Z", ModifiedOn: "2009-11-10T23:00:00Z", CreatedBy: ""}}}
suite.Equal(expusrs2, usrs2)
Expand All @@ -517,7 +517,7 @@ func (suite *AuthTestSuite) TestAuth() {
modified := "2009-11-10T23:00:00Z"

var qUsers1 []User
qUsers1 = append(qUsers1, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, created, modified, ""})
qUsers1 = append(qUsers1, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, "", "", created, modified, ""})
qUsers1 = append(qUsers1, User{
UUID: "uuid7",
Name: "push_worker_0",
Expand All @@ -529,19 +529,19 @@ func (suite *AuthTestSuite) TestAuth() {
ServiceRoles: []string{"push_worker"}, CreatedOn: created, ModifiedOn: modified,
CreatedBy: "",
})
qUsers1 = append(qUsers1, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame2", "", "", "", "", "S3CR3T42", "foo-email", []string{}, created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame1", "", "", "", "", "S3CR3T41", "foo-email", []string{}, created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid4", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic2"}, []string{"sub3", "sub4"}}}, "UserZ", "", "", "", "", "S3CR3T4", "foo-email", []string{}, created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid3", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic3"}, []string{"sub2"}}}, "UserX", "", "", "", "", "S3CR3T3", "foo-email", []string{}, created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid2", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{"topic1", "topic2"}, []string{"sub1", "sub3", "sub4"}}}, "UserB", "", "", "", "", "S3CR3T2", "foo-email", []string{}, created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid1", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{"topic1", "topic2"}, []string{"sub1", "sub2", "sub3"}}}, "UserA", "FirstA", "LastA", "OrgA", "DescA", "S3CR3T1", "foo-email", []string{}, created, modified, ""})
qUsers1 = append(qUsers1, User{"uuid0", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{}, []string{}}}, "Test", "", "", "", "", "S3CR3T", "Test@test.com", []string{}, created, modified, ""})
qUsers1 = append(qUsers1, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame2", "", "", "", "", "S3CR3T42", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame1", "", "", "", "", "S3CR3T41", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid4", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic2"}, []string{"sub3", "sub4"}}}, "UserZ", "", "", "", "", "S3CR3T4", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid3", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic3"}, []string{"sub2"}}}, "UserX", "", "", "", "", "S3CR3T3", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid2", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{"topic1", "topic2"}, []string{"sub1", "sub3", "sub4"}}}, "UserB", "", "", "", "", "S3CR3T2", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers1 = append(qUsers1, User{"uuid1", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{"topic1", "topic2"}, []string{"sub1", "sub2", "sub3"}}}, "UserA", "FirstA", "LastA", "OrgA", "DescA", "S3CR3T1", "foo-email", []string{}, "", "", created, modified, ""})
qUsers1 = append(qUsers1, User{"uuid0", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{}, []string{}}}, "Test", "", "", "", "", "S3CR3T", "Test@test.com", []string{}, "", "", created, modified, ""})
// return all users
pu1, e1 := PaginatedFindUsers(suite.ctx, "", 0, "", true, true, store2)
suite.NoError(e1)

var qUsers2 []User
qUsers2 = append(qUsers2, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, created, modified, ""})
qUsers2 = append(qUsers2, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, "", "", created, modified, ""})
qUsers2 = append(qUsers2, User{
UUID: "uuid7",
Name: "push_worker_0",
Expand All @@ -553,15 +553,15 @@ func (suite *AuthTestSuite) TestAuth() {
ServiceRoles: []string{"push_worker"}, CreatedOn: created, ModifiedOn: modified,
CreatedBy: "",
})
qUsers2 = append(qUsers2, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame2", "", "", "", "", "S3CR3T42", "foo-email", []string{}, created, modified, "UserA"})
qUsers2 = append(qUsers2, User{"same_uuid", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{}, []string{}}}, "UserSame2", "", "", "", "", "S3CR3T42", "foo-email", []string{}, "", "", created, modified, "UserA"})

// return the first page with 2 users
pu2, e2 := PaginatedFindUsers(suite.ctx, "", 3, "", true, true, store2)
suite.NoError(e2)

var qUsers3 []User
qUsers3 = append(qUsers3, User{"uuid4", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic2"}, []string{"sub3", "sub4"}}}, "UserZ", "", "", "", "", "S3CR3T4", "foo-email", []string{}, created, modified, "UserA"})
qUsers3 = append(qUsers3, User{"uuid3", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic3"}, []string{"sub2"}}}, "UserX", "", "", "", "", "S3CR3T3", "foo-email", []string{}, created, modified, "UserA"})
qUsers3 = append(qUsers3, User{"uuid4", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic2"}, []string{"sub3", "sub4"}}}, "UserZ", "", "", "", "", "S3CR3T4", "foo-email", []string{}, "", "", created, modified, "UserA"})
qUsers3 = append(qUsers3, User{"uuid3", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic3"}, []string{"sub2"}}}, "UserX", "", "", "", "", "S3CR3T3", "foo-email", []string{}, "", "", created, modified, "UserA"})
// return the next 2 users
pu3, e3 := PaginatedFindUsers(suite.ctx, "NA==", 2, "", true, true, store2)

Expand All @@ -575,7 +575,7 @@ func (suite *AuthTestSuite) TestAuth() {

// check user list by project and with unprivileged mode (token redacted)
var qUsersC []User
qUsersC = append(qUsersC, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "", "foo-email", []string{}, created, modified, ""})
qUsersC = append(qUsersC, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "", "foo-email", []string{}, "", "", created, modified, ""})

// check for non detailed view
var ndUser []User
Expand Down Expand Up @@ -850,7 +850,7 @@ func (suite *AuthTestSuite) TestGetPushWorker() {

// normal case of push enabled true and correct push worker token
u1, err1 := GetPushWorker(suite.ctx, "push_token", store)
suite.Equal(User{"uuid7", []ProjectRoles{}, "push_worker_0", "", "", "", "", "push_token", "foo-email", []string{"push_worker"}, "2009-11-10T23:00:00Z", "2009-11-10T23:00:00Z", ""}, u1)
suite.Equal(User{"uuid7", []ProjectRoles{}, "push_worker_0", "", "", "", "", "push_token", "foo-email", []string{"push_worker"}, "", "", "2009-11-10T23:00:00Z", "2009-11-10T23:00:00Z", ""}, u1)
suite.Nil(err1)

// incorrect push worker token
Expand Down
86 changes: 51 additions & 35 deletions auth/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ const (

// User is the struct that holds user information
type User struct {
UUID string `json:"uuid"`
Projects []ProjectRoles `json:"projects,omitempty"`
Name string `json:"name"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Organization string `json:"organization,omitempty"`
Description string `json:"description,omitempty"`
Token string `json:"token,omitempty"`
Email string `json:"email"`
ServiceRoles []string `json:"service_roles"`
CreatedOn string `json:"created_on,omitempty"`
ModifiedOn string `json:"modified_on,omitempty"`
CreatedBy string `json:"created_by,omitempty"`
UUID string `json:"uuid"`
Projects []ProjectRoles `json:"projects,omitempty"`
Name string `json:"name"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Organization string `json:"organization,omitempty"`
Description string `json:"description,omitempty"`
Token string `json:"token,omitempty"`
Email string `json:"email"`
ServiceRoles []string `json:"service_roles"`
Component string `json:"component,omitempty"`
ComponentProject string `json:"component_project,omitempty"`
CreatedOn string `json:"created_on,omitempty"`
ModifiedOn string `json:"modified_on,omitempty"`
CreatedBy string `json:"created_by,omitempty"`
}

// ProjectRoles is the struct that hold project and role information of the user
Expand Down Expand Up @@ -241,22 +243,24 @@ func UpdateUserRegistration(ctx context.Context, regUUID, status, declineComment
}

// NewUser accepts parameters and creates a new user
func NewUser(uuid string, projects []ProjectRoles, name string, fname string, lname string, org string, desc string, token string, email string, serviceRoles []string, createdOn time.Time, modifiedOn time.Time, createdBy string) User {
func NewUser(uuid string, projects []ProjectRoles, name string, fname string, lname string, org string, desc string, token string, email string, serviceRoles []string, comp string, compProject string, createdOn time.Time, modifiedOn time.Time, createdBy string) User {
zuluForm := "2006-01-02T15:04:05Z"
return User{
UUID: uuid,
Projects: projects,
Name: name,
FirstName: fname,
LastName: lname,
Organization: org,
Description: desc,
Token: token,
Email: email,
ServiceRoles: serviceRoles,
CreatedOn: createdOn.Format(zuluForm),
ModifiedOn: modifiedOn.Format(zuluForm),
CreatedBy: createdBy}
UUID: uuid,
Projects: projects,
Name: name,
FirstName: fname,
LastName: lname,
Organization: org,
Description: desc,
Token: token,
Email: email,
ServiceRoles: serviceRoles,
Component: comp,
ComponentProject: compProject,
CreatedOn: createdOn.Format(zuluForm),
ModifiedOn: modifiedOn.Format(zuluForm),
CreatedBy: createdBy}
}

// GetPushWorker returns a push worker user by token
Expand Down Expand Up @@ -318,7 +322,7 @@ func GetUserByToken(ctx context.Context, token string, store stores.Store) (User

curUser := NewUser(user.UUID, pRoles, user.Name, user.FirstName,
user.LastName, user.Organization, user.Description, user.Token, user.Email,
user.ServiceRoles, user.CreatedOn.UTC(), user.ModifiedOn.UTC(), usernameC)
user.ServiceRoles, user.Component, user.ComponentProject, user.CreatedOn.UTC(), user.ModifiedOn.UTC(), usernameC)

result = curUser

Expand Down Expand Up @@ -385,7 +389,7 @@ func FindUsers(ctx context.Context, projectUUID string, uuid string, name string
}

curUser := NewUser(item.UUID, pRoles, item.Name, item.FirstName, item.LastName,
item.Organization, item.Description, token, item.Email, serviceRoles,
item.Organization, item.Description, token, item.Email, serviceRoles, item.Component, item.ComponentProject,
item.CreatedOn.UTC(), item.ModifiedOn.UTC(), usernameC)

result.List = append(result.List, curUser)
Expand Down Expand Up @@ -474,7 +478,7 @@ func PaginatedFindUsers(ctx context.Context, pageToken string, pageSize int64, p
}

curUser := NewUser(item.UUID, pRoles, item.Name, item.FirstName, item.LastName,
item.Organization, item.Description, token, item.Email, serviceRoles,
item.Organization, item.Description, token, item.Email, serviceRoles, item.Component, item.ComponentProject,
item.CreatedOn.UTC(), item.ModifiedOn.UTC(), usernameC)

result.Users = append(result.Users, curUser)
Expand Down Expand Up @@ -580,7 +584,7 @@ func GetUserByUUID(ctx context.Context, uuid string, store stores.Store) (User,

curUser := NewUser(user.UUID, pRoles, user.Name, user.FirstName,
user.LastName, user.Organization, user.Description, user.Token, user.Email,
user.ServiceRoles, user.CreatedOn.UTC(), user.ModifiedOn.UTC(), usernameC)
user.ServiceRoles, user.Component, user.ComponentProject, user.CreatedOn.UTC(), user.ModifiedOn.UTC(), usernameC)

result = curUser

Expand All @@ -600,6 +604,18 @@ func GetUUIDByName(ctx context.Context, name string, store stores.Store) string
return result
}

// GetUUIDByComponent queries user by component info and returns the corresponding UUID
func GetUUIDByComponent(ctx context.Context, comp string, compProject string, store stores.Store) string {
result := ""
user, err := store.GetComponentUser(ctx, comp, compProject)

if err == nil && user.UUID != "" {
result = user.UUID
}

return result
}

// UpdateUserToken updates an existing user's token
func UpdateUserToken(ctx context.Context, uuid string, token string, store stores.Store) (User, error) {
if err := store.UpdateUserToken(ctx, uuid, token); err != nil {
Expand Down Expand Up @@ -636,7 +652,7 @@ func AppendToUserProjects(ctx context.Context, userUUID string, projectUUID stri

// UpdateUser updates an existing user's information
// IF the function caller needs to have a view on the updated user object it can set the reflectObj to true
func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, description string, name string, projectList []ProjectRoles, email string, serviceRoles []string, modifiedOn time.Time, reflectObj bool, store stores.Store) (User, error) {
func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, description string, name string, projectList []ProjectRoles, email string, serviceRoles []string, comp string, compProject string, modifiedOn time.Time, reflectObj bool, store stores.Store) (User, error) {

prList := []stores.QProjectRoles{}

Expand Down Expand Up @@ -689,7 +705,7 @@ func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, de
}
}

if err := store.UpdateUser(ctx, uuid, firstName, lastName, organization, description, prList, name, email, serviceRoles, modifiedOn); err != nil {
if err := store.UpdateUser(ctx, uuid, firstName, lastName, organization, description, prList, name, email, serviceRoles, comp, compProject, modifiedOn); err != nil {
return User{}, err
}

Expand All @@ -703,7 +719,7 @@ func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, de
}

// CreateUser creates a new user
func CreateUser(ctx context.Context, uuid string, name string, fname string, lname string, org string, desc string, projectList []ProjectRoles, token string, email string, serviceRoles []string, createdOn time.Time, createdBy string, store stores.Store) (User, error) {
func CreateUser(ctx context.Context, uuid string, name string, fname string, lname string, org string, desc string, projectList []ProjectRoles, token string, email string, serviceRoles []string, comp string, compProject string, createdOn time.Time, createdBy string, store stores.Store) (User, error) {
// check if project with the same name exists
if ExistsWithName(ctx, name, store) {
return User{}, errors.New("exists")
Expand Down Expand Up @@ -755,7 +771,7 @@ func CreateUser(ctx context.Context, uuid string, name string, fname string, lna
}
}

if err := store.InsertUser(ctx, uuid, prList, name, fname, lname, org, desc, token, email, serviceRoles, createdOn, createdOn, createdBy); err != nil {
if err := store.InsertUser(ctx, uuid, prList, name, fname, lname, org, desc, token, email, serviceRoles, comp, compProject, createdOn, createdOn, createdBy); err != nil {
return User{}, errors.New("backend error")
}

Expand Down
Loading
Loading