aboutsummaryrefslogtreecommitdiffstats
path: root/routers/admin/user.go
blob: 9f043507d1bfd16d25ddbec4ca7c36d992ee650b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2014 The Gogs 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 admin

import (
	"strings"

	"github.com/go-martini/martini"

	"github.com/gogits/gogs/models"
	"github.com/gogits/gogs/modules/auth"
	"github.com/gogits/gogs/modules/base"
	"github.com/gogits/gogs/modules/log"
	"github.com/gogits/gogs/modules/middleware"
)

func NewUser(ctx *middleware.Context, form auth.RegisterForm) {
	ctx.Data["Title"] = "New Account"
	ctx.Data["PageIsUsers"] = true

	if ctx.Req.Method == "GET" {
		ctx.HTML(200, "admin/users/new")
		return
	}

	if form.Password != form.RetypePasswd {
		ctx.Data["HasError"] = true
		ctx.Data["Err_Password"] = true
		ctx.Data["Err_RetypePasswd"] = true
		ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
		auth.AssignForm(form, ctx.Data)
	}

	if ctx.HasError() {
		ctx.HTML(200, "admin/users/new")
		return
	}

	u := &models.User{
		Name:     form.UserName,
		Email:    form.Email,
		Passwd:   form.Password,
		IsActive: true,
	}

	var err error
	if u, err = models.RegisterUser(u); err != nil {
		switch err {
		case models.ErrUserAlreadyExist:
			ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form)
		case models.ErrEmailAlreadyUsed:
			ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form)
		case models.ErrUserNameIllegal:
			ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form)
		default:
			ctx.Handle(200, "admin.user.NewUser", err)
		}
		return
	}

	log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI,
		ctx.User.LowerName, strings.ToLower(form.UserName))

	ctx.Redirect("/admin/users")
}

func EditUser(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
	ctx.Data["Title"] = "Edit Account"
	ctx.Data["PageIsUsers"] = true

	uid, err := base.StrTo(params["userid"]).Int()
	if err != nil {
		ctx.Handle(200, "admin.user.EditUser", err)
		return
	}

	u, err := models.GetUserById(int64(uid))
	if err != nil {
		ctx.Handle(200, "admin.user.EditUser", err)
		return
	}

	if ctx.Req.Method == "GET" {
		ctx.Data["User"] = u
		ctx.HTML(200, "admin/users/edit")
		return
	}

	u.Email = form.Email
	u.Website = form.Website
	u.Location = form.Location
	u.Avatar = base.EncodeMd5(form.Avatar)
	u.AvatarEmail = form.Avatar
	u.IsActive = form.Active == "on"
	u.IsAdmin = form.Admin == "on"
	if err := models.UpdateUser(u); err != nil {
		ctx.Handle(200, "admin.user.EditUser", err)
		return
	}

	ctx.Data["IsSuccess"] = true
	ctx.Data["User"] = u
	ctx.HTML(200, "admin/users/edit")

	log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
		ctx.User.LowerName, ctx.User.LowerName)
}

func DeleteUser(ctx *middleware.Context, params martini.Params) {
	ctx.Data["Title"] = "Edit Account"
	ctx.Data["PageIsUsers"] = true

	uid, err := base.StrTo(params["userid"]).Int()
	if err != nil {
		ctx.Handle(200, "admin.user.EditUser", err)
		return
	}

	u, err := models.GetUserById(int64(uid))
	if err != nil {
		ctx.Handle(200, "admin.user.EditUser", err)
		return
	}

	if err = models.DeleteUser(u); err != nil {
		ctx.Data["HasError"] = true
		switch err {
		case models.ErrUserOwnRepos:
			ctx.Data["ErrorMsg"] = "This account still has ownership of repository, owner has to delete or transfer them first."
			ctx.Data["User"] = u
			ctx.HTML(200, "admin/users/edit")
		default:
			ctx.Handle(200, "admin.user.DeleteUser", err)
		}
		return
	}

	log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
		ctx.User.LowerName, ctx.User.LowerName)

	ctx.Redirect("/admin/users")
}