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

Add admin API route for managing user's badges #23106

Merged
merged 25 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ var migrations = []Migration{
NewMigration("Add Actions artifacts expiration date", v1_21.AddExpiredUnixColumnInActionArtifactTable),
// v275 -> v276
NewMigration("Add ScheduleID for ActionRun", v1_21.AddScheduleIDForActionRun),
// v276 -> v277
NewMigration("Use Slug instead of ID for Badges", v1_21.UseSlugInsteadOfIDForBadges),
}

// GetCurrentDBVersion returns the current db version
Expand Down
55 changes: 55 additions & 0 deletions models/migrations/v1_21/v276.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_21 //nolint

import (
"code.gitea.io/gitea/models/migrations/base"

"xorm.io/xorm"
)

type BadgeUnique struct {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Slug string `xorm:"pk UNIQUE"`
Description string
ImageURL string
}

func (BadgeUnique) TableName() string {
return "badge"
}

func UseSlugInsteadOfIDForBadges(x *xorm.Engine) error {
type Badge struct {
Slug string
}
type UserBadge struct {
BadgeSlug string `xorm:"INDEX"`
}
err := x.Sync(new(Badge), new(UserBadge))
if err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
// add slug to each badge
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
_, err = sess.Exec("UPDATE `badge` SET `slug` = `id`")
if err != nil {
return err
}
// update user_badge keys to use slug instead of id
_, err = sess.Exec("UPDATE `user_badge` SET `badge_slug` = (SELECT `slug` FROM `badge` WHERE `badge`.`id` = `user_badge`.`badge_id`)")

if err != nil {
return err
}
// drop badge_id columns from tables
if err := base.DropTableColumns(sess, "user_badge", "badge_id"); err != nil {
return err
}
sess.Commit()
return base.RecreateTables(new(BadgeUnique))(x)
}
81 changes: 76 additions & 5 deletions models/user/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (

// Badge represents a user badge
type Badge struct {
ID int64 `xorm:"pk autoincr"`
Slug string `xorm:"pk UNIQUE"`
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Description string
ImageURL string
}

// UserBadge represents a user badge
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
type UserBadge struct { //nolint:revive
ID int64 `xorm:"pk autoincr"`
BadgeID int64
UserID int64 `xorm:"INDEX"`
ID int64 `xorm:"pk autoincr"`
BadgeSlug string
UserID int64 `xorm:"INDEX"`
}

func init() {
Expand All @@ -32,10 +32,81 @@ func init() {
func GetUserBadges(ctx context.Context, u *User) ([]*Badge, int64, error) {
sess := db.GetEngine(ctx).
Select("`badge`.*").
Join("INNER", "user_badge", "`user_badge`.badge_id=badge.id").
Join("INNER", "user_badge", "`user_badge`.badge_slug=badge.slug").
Where("user_badge.user_id=?", u.ID)

badges := make([]*Badge, 0, 8)
count, err := sess.FindAndCount(&badges)
return badges, count, err
}

// CreateBadge creates a new badge.
func CreateBadge(ctx context.Context, badge *Badge) error {
_, err := db.GetEngine(ctx).Insert(badge)
return err
}

// GetBadge returns a badge
func GetBadge(ctx context.Context, slug string) (*Badge, error) {
badge := new(Badge)
has, err := db.GetEngine(ctx).Where("slug=?", slug).Get(badge)
if !has {
return nil, err
}
return badge, err
}

// UpdateBadge updates a badge based on its slug.
func UpdateBadge(ctx context.Context, badge *Badge) error {
_, err := db.GetEngine(ctx).Where("slug=?", badge.Slug).Update(badge)
return err
}

// DeleteBadge deletes a badge.
func DeleteBadge(ctx context.Context, badge *Badge) error {
_, err := db.GetEngine(ctx).Delete(badge)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// AddUserBadge adds a badge to a user.
func AddUserBadge(ctx context.Context, u *User, badge *Badge) error {
return AddUserBadges(ctx, u, []*Badge{badge})
}

// AddUserBadges adds badges to a user.
func AddUserBadges(ctx context.Context, u *User, badges []*Badge) error {
return db.WithTx(ctx, func(ctx context.Context) error {
for _, badge := range badges {
if err := db.Insert(ctx, &UserBadge{
BadgeSlug: badge.Slug,
UserID: u.ID,
}); err != nil {
return err
}
}
return nil
})
}

// RemoveUserBadge removes a badge from a user.
func RemoveUserBadge(ctx context.Context, u *User, badge *Badge) error {
return RemoveUserBadges(ctx, u, []*Badge{badge})
}

// RemoveUserBadges removes badges from a user.
func RemoveUserBadges(ctx context.Context, u *User, badges []*Badge) error {
return db.WithTx(ctx, func(ctx context.Context) error {
for _, badge := range badges {
if _, err := db.GetEngine(ctx).Where("user_id=? AND badge_slug=?", u.ID, badge.Slug).Delete(&UserBadge{}); err != nil {
return err
}
}
return nil
})
}

// RemoveAllUserBadges removes all badges from a user.
func RemoveAllUserBadges(ctx context.Context, u *User) error {
_, err := db.GetEngine(ctx).Where("user_id=?", u.ID).Delete(&UserBadge{})
return err
}
23 changes: 23 additions & 0 deletions modules/structs/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package structs
Expand Down Expand Up @@ -108,3 +109,25 @@ type UpdateUserAvatarOption struct {
// image must be base64 encoded
Image string `json:"image" binding:"Required"`
}

// Badge represents a user badge
// swagger:model
type Badge struct {
Slug string `json:"slug"`
Description string `json:"description"`
ImageURL string `json:"image_url"`
}

// UserBadge represents a user badge
// swagger:model
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
type UserBadge struct {
ID int64 `json:"id"`
BadgeSlug int64 `json:"badge_slug"`
UserID int64 `json:"user_id"`
}

// UserBadgeOption options for link between users and badges
type UserBadgeOption struct {
// example: ["badge1","badge2"]
BadgeSlugs []string `json:"badge_slugs" binding:"Required"`
}
95 changes: 95 additions & 0 deletions routers/api/v1/admin/user_badge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package admin

import (
"net/http"

user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
)

// AddUserBadges add badges to a user
func AddUserBadges(ctx *context.APIContext) {
// swagger:operation POST /admin/users/{username}/badges admin adminAddUserBadges
// ---
// summary: Add a badge to a user
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of user
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/UserBadgeOption"
// responses:
// "204":
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"

form := web.GetForm(ctx).(*api.UserBadgeOption)
badges := prepareBadgesForReplaceOrAdd(ctx, *form)

if err := user_model.AddUserBadges(ctx, ctx.ContextUser, badges); err != nil {
ctx.Error(http.StatusInternalServerError, "ReplaceUserBadges", err)
return
}

ctx.Status(http.StatusNoContent)
}

// DeleteUserBadges delete a badge from a user
func DeleteUserBadges(ctx *context.APIContext) {
// swagger:operation DELETE /admin/users/{username}/badges admin adminDeleteUserBadges
// ---
// summary: Remove a badge from a user
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of user
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/UserBadgeOption"
// responses:
// "204":
// "$ref": "#/responses/empty"
// "403":
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"

form := web.GetForm(ctx).(*api.UserBadgeOption)
badges := prepareBadgesForReplaceOrAdd(ctx, *form)

if err := user_model.RemoveUserBadges(ctx, ctx.ContextUser, badges); err != nil {
ctx.Error(http.StatusInternalServerError, "ReplaceUserBadges", err)
return
}

ctx.Status(http.StatusNoContent)
}

func prepareBadgesForReplaceOrAdd(ctx *context.APIContext, form api.UserBadgeOption) []*user_model.Badge {
badges := make([]*user_model.Badge, len(form.BadgeSlugs))
for i, badge := range form.BadgeSlugs {
badges[i] = &user_model.Badge{
Slug: badge,
}
}
return badges
}
2 changes: 2 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,8 @@ func Routes() *web.Route {
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
m.Post("/rename", bind(api.RenameUserOption{}), admin.RenameUser)
m.Post("/badges", bind(api.UserBadgeOption{}), admin.AddUserBadges)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
m.Delete("/badges", bind(api.UserBadgeOption{}), admin.DeleteUserBadges)
}, context_service.UserAssignmentAPI())
})
m.Group("/emails", func() {
Expand Down
3 changes: 3 additions & 0 deletions routers/api/v1/swagger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,7 @@ type swaggerParameterBodies struct {

// in:body
CreateOrUpdateSecretOption api.CreateOrUpdateSecretOption

// in:body
UserBadgeOption api.UserBadgeOption
}
Loading