mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
Implemented Badge Management in administration panel (#29798)
Co-authored-by: Diogo Vicente <diogo.m.s.vicente@tecnico.ulisboa.pt>
This commit is contained in:
committed by
Henrique Pimentel
co-authored by
Diogo Vicente
parent
7adf8d7727
commit
8b86c3140a
@@ -25,6 +25,19 @@ type AdminCreateUserForm struct {
|
||||
Visibility structs.VisibleType
|
||||
}
|
||||
|
||||
// AdminCreateBadgeForm form for admin to create badge
|
||||
type AdminCreateBadgeForm struct {
|
||||
Slug string `binding:"Required;Slug"`
|
||||
Description string
|
||||
ImageURL string `binding:"ValidImageUrl"`
|
||||
}
|
||||
|
||||
// Validate validates form fields
|
||||
func (f *AdminCreateBadgeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
|
||||
ctx := context.GetValidateContext(req)
|
||||
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
|
||||
}
|
||||
|
||||
// Validate validates form fields
|
||||
func (f *AdminCreateUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
|
||||
ctx := context.GetValidateContext(req)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
)
|
||||
|
||||
// RenameBadge changes the slug of a badge.
|
||||
func RenameBadge(ctx context.Context, b *user_model.Badge, newSlug string) error {
|
||||
if newSlug == b.Slug {
|
||||
return nil
|
||||
}
|
||||
|
||||
olderSlug := b.Slug
|
||||
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
isExist, err := user_model.IsBadgeExist(ctx, b.ID, newSlug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isExist {
|
||||
return user_model.ErrBadgeAlreadyExist{
|
||||
Slug: newSlug,
|
||||
}
|
||||
}
|
||||
|
||||
b.Slug = newSlug
|
||||
if err := user_model.UpdateBadge(ctx, b); err != nil {
|
||||
b.Slug = olderSlug
|
||||
return err
|
||||
}
|
||||
if err = committer.Commit(); err != nil {
|
||||
b.Slug = olderSlug
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteBadge completely and permanently deletes everything of a badge
|
||||
func DeleteBadge(ctx context.Context, b *user_model.Badge, purge bool) error {
|
||||
if purge {
|
||||
err := user_model.DeleteUserBadgeRecord(ctx, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
if err := user_model.DeleteBadge(ctx, b); err != nil {
|
||||
return fmt.Errorf("DeleteBadge: %w", err)
|
||||
}
|
||||
|
||||
if err := committer.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = committer.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user