2018-12-06 22:58:02 -05:00
|
|
|
// Copyright 2018 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 context
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/db"
|
2018-12-06 22:58:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ParamsUser is the wrapper type of the target user defined by URL parameter, namely ':username'.
|
|
|
|
|
type ParamsUser struct {
|
2019-10-24 01:51:46 -07:00
|
|
|
*db.User
|
2018-12-06 22:58:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InjectParamsUser returns a handler that retrieves target user based on URL parameter ':username',
|
|
|
|
|
// and injects it as *ParamsUser.
|
|
|
|
|
func InjectParamsUser() macaron.Handler {
|
|
|
|
|
return func(c *Context) {
|
2022-11-05 23:33:05 +08:00
|
|
|
user, err := db.Users.GetByUsername(c.Req.Context(), c.Params(":username"))
|
2018-12-06 22:58:02 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NotFoundOrError(err, "get user by name")
|
2018-12-06 22:58:02 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Map(&ParamsUser{user})
|
|
|
|
|
}
|
|
|
|
|
}
|