Skip to content

Commit

Permalink
API for users to new model
Browse files Browse the repository at this point in the history
This commit moves the legacy apis related to users to new model.
Some funcs under common/dao are left b/c they are used by other module,
which should also be shifted to leverage managers.
We'll handle them separately.

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
  • Loading branch information
reasonerjt committed Apr 13, 2021
1 parent 3646b26 commit d4cd2b8
Show file tree
Hide file tree
Showing 35 changed files with 2,610 additions and 1,864 deletions.
433 changes: 0 additions & 433 deletions api/v2.0/legacy_swagger.yaml

Large diffs are not rendered by default.

450 changes: 448 additions & 2 deletions api/v2.0/swagger.yaml

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/common/security/local/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"github.com/goharbor/harbor/src/pkg/permission/types"
)

// ContextName the name of the security context.
const ContextName = "local"

// SecurityContext implements security.Context interface based on database
type SecurityContext struct {
user *models.User
Expand All @@ -44,7 +47,7 @@ func NewSecurityContext(user *models.User) *SecurityContext {

// Name returns the name of the security context
func (s *SecurityContext) Name() string {
return "local"
return ContextName
}

// IsAuthenticated returns true if the user has been authenticated
Expand Down
5 changes: 3 additions & 2 deletions src/controller/project/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package project
import (
"context"

commonmodels "github.com/goharbor/harbor/src/common/models"
event "github.com/goharbor/harbor/src/controller/event/metadata"
"github.com/goharbor/harbor/src/controller/event/operator"
"github.com/goharbor/harbor/src/lib/errors"
Expand Down Expand Up @@ -61,7 +62,7 @@ type Controller interface {
// Update update the project
Update(ctx context.Context, project *models.Project) error
// ListRoles lists the roles of user for the specific project
ListRoles(ctx context.Context, projectID int64, u *user.User) ([]int, error)
ListRoles(ctx context.Context, projectID int64, u *commonmodels.User) ([]int, error)
}

// NewController creates an instance of the default project controller
Expand Down Expand Up @@ -241,7 +242,7 @@ func (c *controller) Update(ctx context.Context, p *models.Project) error {
return nil
}

func (c *controller) ListRoles(ctx context.Context, projectID int64, u *user.User) ([]int, error) {
func (c *controller) ListRoles(ctx context.Context, projectID int64, u *commonmodels.User) ([]int, error) {
if u == nil {
return nil, nil
}
Expand Down
134 changes: 134 additions & 0 deletions src/controller/user/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package user

import (
"context"
"fmt"

"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/security/local"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/oidc"
"github.com/goharbor/harbor/src/pkg/user"
"github.com/goharbor/harbor/src/pkg/user/models"
)

var (
// Ctl is a global user controller instance
Ctl = NewController()
)

// Controller provides functions to support API/middleware for user management and query
type Controller interface {
// SetSysAdmin ...
SetSysAdmin(ctx context.Context, id int, adminFlag bool) error
// VerifyPassword ...
VerifyPassword(ctx context.Context, username string, password string) (bool, error)
// UpdatePassword ...
UpdatePassword(ctx context.Context, id int, password string) error
// List ...
List(ctx context.Context, query *q.Query) ([]*models.User, error)
// Create ...
Create(ctx context.Context, u *models.User) (int, error)
// Count ...
Count(ctx context.Context, query *q.Query) (int64, error)
// Get ...
Get(ctx context.Context, id int, opt *Option) (*models.User, error)
// Delete ...
Delete(ctx context.Context, id int) error
// UpdateProfile update the profile based on the ID and data in the model in parm, only a subset of attributes in the model
// will be update, see the implementation of manager.
UpdateProfile(ctx context.Context, u *models.User) error
// SetCliSecret sets the OIDC CLI secret for a user
SetCliSecret(ctx context.Context, id int, secret string) error
}

// NewController ...
func NewController() Controller {
return &controller{
mgr: user.New(),
oidcMetaMgr: oidc.NewMetaMgr(),
}
}

// Option option for getting User info
type Option struct {
WithOIDCInfo bool
}

type controller struct {
mgr user.Manager
oidcMetaMgr oidc.MetaManager
}

func (c *controller) SetCliSecret(ctx context.Context, id int, secret string) error {
return c.oidcMetaMgr.SetCliSecretByUserID(ctx, id, secret)
}

func (c *controller) Create(ctx context.Context, u *models.User) (int, error) {
return c.mgr.Create(ctx, u)
}

func (c *controller) UpdateProfile(ctx context.Context, u *models.User) error {
return c.mgr.UpdateProfile(ctx, u)
}

func (c *controller) Get(ctx context.Context, id int, opt *Option) (*models.User, error) {
u, err := c.mgr.Get(ctx, id)
if err != nil {
return nil, err
}
sctx, ok := security.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("can't find security context")
}
lsc, ok := sctx.(*local.SecurityContext)
if ok && lsc.User().UserID == id {
u.AdminRoleInAuth = lsc.User().AdminRoleInAuth
}
if opt != nil && opt.WithOIDCInfo {
oidcMeta, err := c.oidcMetaMgr.GetByUserID(ctx, id)
if err != nil {
return nil, err
}
u.OIDCUserMeta = oidcMeta
}
return u, nil
}

func (c *controller) Count(ctx context.Context, query *q.Query) (int64, error) {
return c.mgr.Count(ctx, query)
}

func (c *controller) Delete(ctx context.Context, id int) error {
return c.mgr.Delete(ctx, id)
}

func (c *controller) List(ctx context.Context, query *q.Query) ([]*models.User, error) {
return c.mgr.List(ctx, query)
}

func (c *controller) UpdatePassword(ctx context.Context, id int, password string) error {
return c.mgr.UpdatePassword(ctx, id, password)
}

func (c *controller) VerifyPassword(ctx context.Context, username, password string) (bool, error) {
return c.mgr.VerifyLocalPassword(ctx, username, password)
}

func (c *controller) SetSysAdmin(ctx context.Context, id int, adminFlag bool) error {
return c.mgr.SetSysAdminFlag(ctx, id, adminFlag)
}
7 changes: 0 additions & 7 deletions src/core/api/harborapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,11 @@ func init() {
beego.TestBeegoInit(apppath)

beego.Router("/api/health", &HealthAPI{}, "get:CheckHealth")
beego.Router("/api/users/:id", &UserAPI{}, "get:Get")
beego.Router("/api/users", &UserAPI{}, "get:List;post:Post;delete:Delete;put:Put")
beego.Router("/api/users/search", &UserAPI{}, "get:Search")
beego.Router("/api/users/:id([0-9]+)/password", &UserAPI{}, "put:ChangePassword")
beego.Router("/api/users/:id/permissions", &UserAPI{}, "get:ListUserPermissions")
beego.Router("/api/users/:id/sysadmin", &UserAPI{}, "put:ToggleUserAdminRole")
beego.Router("/api/projects/:id([0-9]+)/metadatas/?:name", &MetadataAPI{}, "get:Get")
beego.Router("/api/projects/:id([0-9]+)/metadatas/", &MetadataAPI{}, "post:Post")
beego.Router("/api/projects/:id([0-9]+)/metadatas/:name", &MetadataAPI{}, "put:Put;delete:Delete")
beego.Router("/api/projects/:pid([0-9]+)/members/?:pmid([0-9]+)", &ProjectMemberAPI{})
beego.Router("/api/statistics", &StatisticAPI{})
beego.Router("/api/users/?:id", &UserAPI{})
beego.Router("/api/email/ping", &EmailAPI{}, "post:Ping")
beego.Router("/api/labels", &LabelAPI{}, "post:Post;get:List")
beego.Router("/api/labels/:id([0-9]+", &LabelAPI{}, "get:Get;put:Put;delete:Delete")
Expand Down
Loading

0 comments on commit d4cd2b8

Please sign in to comment.