mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-25 17:13:03 -04:00
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
**AI Disclosure:** This work was produced with the assistance of an artificial intelligence tool
## feat: Add admin endpoints for individual user email management
Add GET and DELETE endpoints at `/admin/users/{username}/emails` to allow
administrators to list and delete individual email addresses for users.
These API endpoints provide programmatic access to functionality that is
currently only available through the web UI:
- http://forgejo.example/admin/emails (delete individual email addresses)
- http://forgejo.example/admin/users/1 (view individual user's emails)
The new endpoints follow existing admin API naming patterns such as
`/admin/users/{username}/keys`, `/admin/users/{username}/orgs`, and
`/admin/users/{username}/quota`, providing consistent resource management
under the `/admin/users/{username}` namespace.
This complements the existing `/admin/emails` endpoint which lists all emails
across all users, providing administrators with granular control over
individual user email management.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests
- I added test coverage for Go changes...
- [ ] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9594
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Ryan Lerch <rlerch@redhat.com>
Co-committed-by: Ryan Lerch <rlerch@redhat.com>
250 lines
6.2 KiB
Go
250 lines
6.2 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
auth_model "forgejo.org/models/auth"
|
|
"forgejo.org/models/db"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/util"
|
|
"forgejo.org/modules/validation"
|
|
"forgejo.org/services/mailer"
|
|
)
|
|
|
|
// AdminAddOrSetPrimaryEmailAddress is used by admins to add or set a user's primary email address
|
|
func AdminAddOrSetPrimaryEmailAddress(ctx context.Context, u *user_model.User, emailStr string) error {
|
|
if strings.EqualFold(u.Email, emailStr) {
|
|
return nil
|
|
}
|
|
|
|
if err := validation.ValidateEmailForAdmin(emailStr); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if address exists already
|
|
email, err := user_model.GetEmailAddressByEmail(ctx, emailStr)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return err
|
|
}
|
|
if email != nil && email.UID != u.ID {
|
|
return user_model.ErrEmailAlreadyUsed{Email: emailStr}
|
|
}
|
|
|
|
// Update old primary address
|
|
primary, err := user_model.GetPrimaryEmailAddressOfUser(ctx, u.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
primary.IsPrimary = false
|
|
if err := user_model.UpdateEmailAddress(ctx, primary); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Insert new or update existing address
|
|
if email != nil {
|
|
email.IsPrimary = true
|
|
email.IsActivated = true
|
|
if err := user_model.UpdateEmailAddress(ctx, email); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
email = &user_model.EmailAddress{
|
|
UID: u.ID,
|
|
Email: emailStr,
|
|
IsActivated: true,
|
|
IsPrimary: true,
|
|
}
|
|
if _, err := user_model.InsertEmailAddress(ctx, email); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
u.Email = emailStr
|
|
|
|
return user_model.UpdateUserCols(ctx, u, "email")
|
|
}
|
|
|
|
func ReplacePrimaryEmailAddress(ctx context.Context, u *user_model.User, emailStr string) error {
|
|
if strings.EqualFold(u.Email, emailStr) {
|
|
return nil
|
|
}
|
|
|
|
if err := validation.ValidateEmail(emailStr); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !u.IsOrganization() {
|
|
// Check if address exists already
|
|
email, err := user_model.GetEmailAddressByEmail(ctx, emailStr)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return err
|
|
}
|
|
if email != nil {
|
|
if email.IsPrimary && email.UID == u.ID {
|
|
return nil
|
|
}
|
|
return user_model.ErrEmailAlreadyUsed{Email: emailStr}
|
|
}
|
|
|
|
// Remove old primary address
|
|
primary, err := user_model.GetPrimaryEmailAddressOfUser(ctx, u.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := db.DeleteByID[user_model.EmailAddress](ctx, primary.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Insert new primary address
|
|
email = &user_model.EmailAddress{
|
|
UID: u.ID,
|
|
Email: emailStr,
|
|
IsActivated: true,
|
|
IsPrimary: true,
|
|
}
|
|
if _, err := user_model.InsertEmailAddress(ctx, email); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
u.Email = emailStr
|
|
|
|
return user_model.UpdateUserCols(ctx, u, "email")
|
|
}
|
|
|
|
func AddEmailAddresses(ctx context.Context, u *user_model.User, emails []string) error {
|
|
for _, emailStr := range emails {
|
|
if err := validation.ValidateEmail(emailStr); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if address exists already
|
|
email, err := user_model.GetEmailAddressByEmail(ctx, emailStr)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
return err
|
|
}
|
|
if email != nil {
|
|
return user_model.ErrEmailAlreadyUsed{Email: emailStr}
|
|
}
|
|
|
|
// Insert new address
|
|
email = &user_model.EmailAddress{
|
|
UID: u.ID,
|
|
Email: emailStr,
|
|
IsActivated: !setting.Service.RegisterEmailConfirm,
|
|
IsPrimary: false,
|
|
}
|
|
if _, err := user_model.InsertEmailAddress(ctx, email); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ReplaceInactivePrimaryEmail replaces the primary email of a given user, even if the primary is not yet activated.
|
|
func ReplaceInactivePrimaryEmail(ctx context.Context, oldEmail string, email *user_model.EmailAddress) error {
|
|
user := &user_model.User{}
|
|
has, err := db.GetEngine(ctx).ID(email.UID).Get(user)
|
|
if err != nil {
|
|
return err
|
|
} else if !has {
|
|
return user_model.ErrUserNotExist{
|
|
UID: email.UID,
|
|
}
|
|
}
|
|
|
|
err = AddEmailAddresses(ctx, user, []string{email.Email})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = MakeEmailAddressPrimary(ctx, user, email, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Delete previous activation token.
|
|
if err := auth_model.DeleteAuthTokenByUser(ctx, user.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return DeleteEmailAddresses(ctx, user, []string{oldEmail})
|
|
}
|
|
|
|
func DeleteEmailAddresses(ctx context.Context, u *user_model.User, emails []string) error {
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
for _, emailStr := range emails {
|
|
// Check if address exists
|
|
email, err := user_model.GetEmailAddressOfUser(ctx, emailStr, u.ID)
|
|
if err != nil {
|
|
if user_model.IsErrEmailAddressNotExist(err) {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
if email.IsPrimary {
|
|
return user_model.ErrPrimaryEmailCannotDelete{Email: emailStr}
|
|
}
|
|
|
|
// Remove address
|
|
if _, err := db.DeleteByID[user_model.EmailAddress](ctx, email.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func MakeEmailAddressPrimary(ctx context.Context, u *user_model.User, newPrimaryEmail *user_model.EmailAddress, notify bool) error {
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
sess := db.GetEngine(ctx)
|
|
|
|
oldPrimaryEmail := u.Email
|
|
|
|
// If the user was reported as abusive, a shadow copy should be created before first update (of certain columns).
|
|
if err = user_model.IfNeededCreateShadowCopyForUser(ctx, u.ID, u, "email"); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 1. Update user table
|
|
u.Email = newPrimaryEmail.Email
|
|
if _, err = sess.ID(u.ID).Cols("email").Update(u); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 2. Update old primary email
|
|
if _, err = sess.Where("uid=? AND is_primary=?", u.ID, true).Cols("is_primary").Update(&user_model.EmailAddress{
|
|
IsPrimary: false,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 3. update new primary email
|
|
newPrimaryEmail.IsPrimary = true
|
|
if _, err = sess.ID(newPrimaryEmail.ID).Cols("is_primary").Update(newPrimaryEmail); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := committer.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if notify {
|
|
return mailer.SendPrimaryMailChange(u, oldPrimaryEmail)
|
|
}
|
|
return nil
|
|
}
|