Skip to content

Commit

Permalink
Merge pull request #511 from traPtitech/fix/issue-275
Browse files Browse the repository at this point in the history
grant priviledge API
  • Loading branch information
ras0q authored Jan 5, 2024
2 parents 8715875 + de153b6 commit 1bf18fe
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ paths:
responses:
'200':
$ref: '#/components/responses/icalSecret'
/users/{userID}/privileged:
parameters:
- $ref: '#/components/parameters/userID'
patch:
tags:
- users
operationId: grantPrivilege
description: 管理者権限を付与したいuserのuserIDをパラメータに入れる. APIを叩く本人が管理者権限を持っている必要がある.
responses:
'204':
$ref: '#/components/responses/Nocontent'
'400':
description: Bad Request
'403':
description: Forbidden
'404':
description: Not Found

/tags:
get:
Expand Down
1 change: 1 addition & 0 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ type UserRepository interface {
GetMyiCalSecret(info *ConInfo) (string, error)

IsPrevilege(info *ConInfo) bool
GrantPrivilege(userID uuid.UUID) error
SyncUsers(info *ConInfo) error
}
10 changes: 10 additions & 0 deletions infra/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ func getAllUsers(db *gorm.DB, onlyActive bool) ([]*User, error) {
err := db.Find(&users).Error
return users, err
}

func (repo *GormRepository) GrantPrivilege(userID uuid.UUID) error {
err := grantPrivilege(repo.db, userID)
return defaultErrorHandling(err)
}

func grantPrivilege(db *gorm.DB, userID uuid.UUID) error {
err := db.Model(&User{ID: userID}).Update("privilege", true).Error
return err
}
13 changes: 13 additions & 0 deletions repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"errors"
"fmt"

"github.com/gofrs/uuid"
"github.com/traPtitech/go-traq"
Expand Down Expand Up @@ -206,3 +207,15 @@ func (repo *Repository) mergeUser(userMeta *db.User, userBody *traq.User) (*doma
State: userMeta.State,
}, nil
}

func (repo *Repository) GrantPrivilege(userID uuid.UUID) error {
user, err := repo.GormRepo.GetUser(userID)
if err != nil {
return defaultErrorHandling(err)
}
if user.Privilege {
return fmt.Errorf("%w: user has been already privileged", domain.ErrBadRequest)
}
err = repo.GormRepo.GrantPrivilege(userID)
return defaultErrorHandling(err)
}
1 change: 1 addition & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (h *Handlers) SetupRoute() *echo.Echo {
// サービス管理者権限が必要
usersAPIWithPrevilegeAuth := usersAPI.Group("", h.PrevilegeUserMiddleware)
{
usersAPIWithPrevilegeAuth.PATCH("/:userid/privileged", h.HandleGrantPrivilege)
usersAPIWithPrevilegeAuth.POST("/sync", h.HandleSyncUser)
}
}
Expand Down
13 changes: 13 additions & 0 deletions router/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ func (h *Handlers) HandleSyncUser(c echo.Context) error {

return c.NoContent(http.StatusCreated)
}

// 権限のあるユーザーがないユーザーに権限を付与
func (h *Handlers) HandleGrantPrivilege(c echo.Context) error {
userID, err := getPathUserID(c)
if err != nil {
return notFound(err)
}
err = h.Repo.GrantPrivilege(userID)
if err != nil {
return judgeErrorResponse(err)
}
return c.NoContent(http.StatusCreated)
}

0 comments on commit 1bf18fe

Please sign in to comment.