2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-28 06:30:04 +01:00
|
|
|
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/server/api/payload"
|
|
|
|
"github.com/miniflux/miniflux/server/core"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateUser is the API handler to create a new user.
|
|
|
|
func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
if !ctx.IsAdminUser() {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Forbidden()
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
user, err := payload.DecodeUserPayload(request.Body())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.ValidateUserCreation(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.store.UserExists(user.Username) {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(errors.New("This user already exists"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.store.CreateUser(user)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to create this user"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = ""
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Created(user)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser is the API handler to update the given user.
|
|
|
|
func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
if !ctx.IsAdminUser() {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Forbidden()
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
userID, err := request.IntegerParam("userID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
user, err := payload.DecodeUserPayload(request.Body())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.ValidateUserModification(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
originalUser, err := c.store.UserByID(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(errors.New("Unable to fetch this user from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if originalUser == nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NotFound(errors.New("User not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
originalUser.Merge(user)
|
|
|
|
if err = c.store.UpdateUser(originalUser); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to update this user"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Created(originalUser)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers is the API handler to get the list of users.
|
|
|
|
func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
if !ctx.IsAdminUser() {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Forbidden()
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
users, err := c.store.Users()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch the list of users"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Standard(users)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser is the API handler to fetch the given user.
|
|
|
|
func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
if !ctx.IsAdminUser() {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Forbidden()
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
userID, err := request.IntegerParam("userID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
user, err := c.store.UserByID(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(errors.New("Unable to fetch this user from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NotFound(errors.New("User not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Standard(user)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUser is the API handler to remove an existing user.
|
|
|
|
func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) {
|
|
|
|
if !ctx.IsAdminUser() {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Forbidden()
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
userID, err := request.IntegerParam("userID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
user, err := c.store.UserByID(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch this user from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NotFound(errors.New("User not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.store.RemoveUser(user.ID); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(errors.New("Unable to remove this user from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NoContent()
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|