Skip to content

Commit

Permalink
Merge pull request #351 from ca-risken/modify-list-user
Browse files Browse the repository at this point in the history
add user_idp_key for ListUser param
  • Loading branch information
iiiidaaa committed Dec 7, 2023
2 parents 76ffb04 + 5f5ca8d commit adc99d9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
8 changes: 6 additions & 2 deletions pkg/db/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type IAMRepository interface {
// User
ListUser(ctx context.Context, activated bool, projectID uint32, name string, userID uint32, admin bool) (*[]model.User, error)
ListUser(ctx context.Context, activated bool, projectID uint32, name string, userID uint32, admin bool, userIdpKey string) (*[]model.User, error)
GetUser(ctx context.Context, userID uint32, sub string) (*model.User, error)
GetUserBySub(ctx context.Context, sub string) (*model.User, error)
CreateUser(ctx context.Context, u *model.User) (*model.User, error)
Expand Down Expand Up @@ -62,7 +62,7 @@ type IAMRepository interface {

var _ IAMRepository = (*Client)(nil)

func (c *Client) ListUser(ctx context.Context, activated bool, projectID uint32, name string, userID uint32, admin bool) (*[]model.User, error) {
func (c *Client) ListUser(ctx context.Context, activated bool, projectID uint32, name string, userID uint32, admin bool, userIdpKey string) (*[]model.User, error) {
query := `
select
u.*
Expand All @@ -89,6 +89,10 @@ where
if admin {
query += " and exists (select * from user_role ur where ur.user_id = u.user_id and ur.project_id is null)"
}
if userIdpKey != "" {
query += " and u.user_idp_key = ?"
params = append(params, userIdpKey)
}
var data []model.User
if err := c.Slave.WithContext(ctx).Raw(query, params...).Scan(&data).Error; err != nil {
return nil, err
Expand Down
18 changes: 9 additions & 9 deletions pkg/db/mocks/IAMRepository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/server/iam/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (i *IAMService) ListUser(ctx context.Context, req *iam.ListUserRequest) (*i
if err := req.Validate(); err != nil {
return nil, err
}
list, err := i.repository.ListUser(ctx, req.Activated, req.ProjectId, req.Name, req.UserId, req.Admin)
list, err := i.repository.ListUser(ctx, req.Activated, req.ProjectId, req.Name, req.UserId, req.Admin, req.UserIdpKey)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &iam.ListUserResponse{}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/iam/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestListUser(t *testing.T) {
svc := IAMService{repository: mock}

if c.mockResponce != nil || c.mockError != nil {
mock.On("ListUser", test.RepeatMockAnything(6)...).Return(c.mockResponce, c.mockError).Once()
mock.On("ListUser", test.RepeatMockAnything(7)...).Return(c.mockResponce, c.mockError).Once()
}
got, err := svc.ListUser(ctx, c.input)
if err != nil && !c.wantErr {
Expand Down
68 changes: 39 additions & 29 deletions proto/iam/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/iam/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message ListUserRequest {
bool activated = 3;
uint32 user_id = 4;
bool admin = 5;
string user_idp_key = 6;
}

message ListUserResponse { repeated uint32 user_id = 1; }
Expand Down

0 comments on commit adc99d9

Please sign in to comment.