ripple-api/app/v1/manage_user.go

49 lines
1.2 KiB
Go
Raw Normal View History

2016-04-12 19:41:08 +00:00
package v1
import (
"time"
2017-01-14 17:06:16 +00:00
"zxq.co/ripple/rippleapi/common"
)
2016-04-12 19:41:08 +00:00
type setAllowedData struct {
UserID int `json:"user_id"`
Allowed int `json:"allowed"`
}
// UserManageSetAllowedPOST allows to set the allowed status of an user.
2016-04-16 16:05:24 +00:00
func UserManageSetAllowedPOST(md common.MethodData) common.CodeMessager {
2016-04-12 19:41:08 +00:00
data := setAllowedData{}
if err := md.RequestData.Unmarshal(&data); err != nil {
2016-04-16 16:05:24 +00:00
return ErrBadJSON
2016-04-12 19:41:08 +00:00
}
if data.Allowed < 0 || data.Allowed > 2 {
2016-04-16 16:05:24 +00:00
return common.SimpleResponse(400, "Allowed status must be between 0 and 2")
2016-04-12 19:41:08 +00:00
}
var banDatetime int64
var privsSet string
if data.Allowed == 0 {
banDatetime = time.Now().Unix()
privsSet = "privileges = (privileges & ~3)"
} else {
banDatetime = 0
privsSet = "privileges = (privileges | 3)"
}
_, err := md.DB.Exec("UPDATE users SET "+privsSet+", ban_datetime = ? WHERE id = ?", banDatetime, data.UserID)
2016-04-12 19:41:08 +00:00
if err != nil {
md.Err(err)
2016-04-16 16:05:24 +00:00
return Err500
2016-04-12 19:41:08 +00:00
}
go fixPrivileges(data.UserID, md.DB)
2016-04-12 19:41:08 +00:00
query := `
SELECT users.id, users.username, register_datetime, privileges,
2016-04-12 19:41:08 +00:00
latest_activity, users_stats.username_aka,
users_stats.country
2016-04-12 19:41:08 +00:00
FROM users
LEFT JOIN users_stats
ON users.id=users_stats.id
WHERE users.id=?
LIMIT 1`
return userPutsSingle(md, md.DB.QueryRowx(query, data.UserID))
2016-04-12 19:41:08 +00:00
}