Skip to content

Add option to change username to the admin panel #2129

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
81 changes: 81 additions & 0 deletions integrations/admin_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"net/http"
"strconv"
"testing"

"code.gitea.io/gitea/models"
"github.com/stretchr/testify/assert"
)

func TestAdminViewUsers(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user1")
req := NewRequest(t, "GET", "/admin/users")
session.MakeRequest(t, req, http.StatusOK)

session = loginUser(t, "user2")
req = NewRequest(t, "GET", "/admin/users")
session.MakeRequest(t, req, http.StatusForbidden)
}

func TestAdminViewUser(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user1")
req := NewRequest(t, "GET", "/admin/users/1")
session.MakeRequest(t, req, http.StatusOK)

session = loginUser(t, "user2")
req = NewRequest(t, "GET", "/admin/users/1")
session.MakeRequest(t, req, http.StatusForbidden)
}

func TestAdminEditUser(t *testing.T) {
prepareTestEnv(t)

testSuccessfullEdit(t, models.User{ID: 2, Name: "newusername", LoginName: "otherlogin", Email: "[email protected]"})
}

func testSuccessfullEdit(t *testing.T, formData models.User) {
makeRequest(t, formData, http.StatusFound)
}

func makeRequest(t *testing.T, formData models.User, headerCode int) {
session := loginUser(t, "user1")
csrf := GetCSRF(t, session, "/admin/users/"+strconv.Itoa(int(formData.ID)))
req := NewRequestWithValues(t, "POST", "/admin/users/"+strconv.Itoa(int(formData.ID)), map[string]string{
"_csrf": csrf,
"user_name": formData.Name,
"login_name": formData.LoginName,
"login_type": "0-0",
"email": formData.Email,
})

session.MakeRequest(t, req, headerCode)
user := models.AssertExistsAndLoadBean(t, &models.User{ID: formData.ID}).(*models.User)
assert.Equal(t, formData.Name, user.Name)
assert.Equal(t, formData.LoginName, user.LoginName)
assert.Equal(t, formData.Email, user.Email)
}

func TestAdminDeleteUser(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user1")

csrf := GetCSRF(t, session, "/admin/users/8")
req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
"_csrf": csrf,
})
session.MakeRequest(t, req, http.StatusOK)

models.AssertNotExistsBean(t, &models.User{ID: 8})
models.CheckConsistencyFor(t, &models.User{})
}
1 change: 1 addition & 0 deletions modules/auth/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (f *AdminCreateUserForm) Validate(ctx *macaron.Context, errs binding.Errors
// AdminEditUserForm form for admin to create user
type AdminEditUserForm struct {
LoginType string `binding:"Required"`
UserName string `binding:"AlphaDashDot;MaxSize(35)"`
LoginName string
FullName string `binding:"MaxSize(100)"`
Email string `binding:"Required;Email;MaxSize(254)"`
Expand Down
2 changes: 2 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,7 @@ function initAdmin() {
$('.admin.edit.user').length > 0) {
$('#login_type').change(function () {
if ($(this).val().substring(0, 1) == '0') {
$('#user_name').removeAttr('disabled');
$('#login_name').removeAttr('required');
$('.non-local').hide();
$('.local').show();
Expand All @@ -1503,6 +1504,7 @@ function initAdmin() {
}

} else {
$('#user_name').attr('disabled', 'disabled');
$('#login_name').attr('required', 'required');
$('.non-local').show();
$('.local').hide();
Expand Down
8 changes: 8 additions & 0 deletions routers/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
u.HashPassword(form.Password)
}

if u.IsLocal() && len(form.UserName) > 0 && u.Name != form.UserName {
if err := models.ChangeUserName(u, form.UserName); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think models.ChangeUserName and models.UpdateUser should be with a transaction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bwko any update for this?

ctx.Handle(500, "ChangeUserName", err)
return
}
u.Name = form.UserName
}

u.LoginName = form.LoginName
u.FullName = form.FullName
u.Email = form.Email
Expand Down
4 changes: 2 additions & 2 deletions templates/admin/user/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<div class="ui attached segment">
<form class="ui form" action="{{.Link}}" method="post">
{{.CsrfTokenHtml}}
<div class="inline field {{if .Err_UserName}}error{{end}}">
<div class="field {{if .Err_UserName}}error{{end}}">
<label for="user_name">{{.i18n.Tr "username"}}</label>
<span>{{.User.Name}}</span>
<input id="user_name" name="user_name" value="{{.User.Name}}" autofocus {{if not .User.IsLocal }}disabled{{end}}>
</div>
<!-- Types and name -->
<div class="inline required field {{if .Err_LoginType}}error{{end}}">
Expand Down