2016-04-12 19:41:08 +00:00
|
|
|
package v1
|
|
|
|
|
2016-06-06 10:27:54 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.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
|
|
|
}
|
2016-06-06 10:27:54 +00:00
|
|
|
var banDatetime int64
|
2016-07-03 19:55:03 +00:00
|
|
|
var privileges int32
|
|
|
|
var newPrivileges int32
|
|
|
|
err := md.DB.QueryRow("SELECT privileges FROM users WHERE id = ?", data.UserID).Scan(&privileges)
|
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
2016-06-06 10:27:54 +00:00
|
|
|
if data.Allowed == 0 {
|
|
|
|
banDatetime = time.Now().Unix()
|
2016-07-03 22:06:23 +00:00
|
|
|
newPrivileges = privileges &^ (common.UserPrivilegeNormal | common.UserPrivilegePublic)
|
2016-07-03 19:55:03 +00:00
|
|
|
} else {
|
|
|
|
banDatetime = 0
|
|
|
|
newPrivileges = privileges | (common.UserPrivilegeNormal | common.UserPrivilegePublic)
|
2016-06-06 10:27:54 +00:00
|
|
|
}
|
2016-07-03 19:55:03 +00:00
|
|
|
_, err = md.DB.Exec("UPDATE users SET privileges = ?, ban_datetime = ? WHERE id = ?", newPrivileges, 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
|
|
|
}
|
2016-06-14 09:32:11 +00:00
|
|
|
go fixPrivileges(data.UserID, md.DB)
|
2016-04-12 19:41:08 +00:00
|
|
|
query := `
|
2016-07-03 22:06:23 +00:00
|
|
|
SELECT users.id, users.username, register_datetime, privileges,
|
2016-04-12 19:41:08 +00:00
|
|
|
latest_activity, users_stats.username_aka,
|
2016-07-06 21:43:43 +00:00
|
|
|
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`
|
2016-08-15 17:59:46 +00:00
|
|
|
return userPuts(md, md.DB.QueryRowx(query, data.UserID))
|
2016-04-12 19:41:08 +00:00
|
|
|
}
|