fix: add role limit
This commit is contained in:
@@ -1,26 +1,40 @@
|
||||
package article
|
||||
|
||||
import (
|
||||
"nCovTrack-Backend/global"
|
||||
"nCovTrack-Backend/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
//ListPublishedArticles list the articles published, use to show the articles to all people
|
||||
func ListPublishedArticles() *[]map[string]interface{} {
|
||||
article := models.ListField[models.BackArticle]([]map[string]interface{}{{"is_publish": 0}}, true, "content")
|
||||
if *article == nil {
|
||||
article = &[]map[string]interface{}{}
|
||||
}
|
||||
return article
|
||||
func ListPublishedArticles() *[]models.ListArtile {
|
||||
return listArticles(1, 0)
|
||||
}
|
||||
|
||||
//ListAllArticles list all articles, will show the articles not published of the user
|
||||
func ListPublishedArticlesByUser(id int) *[]models.ListArtile {
|
||||
return listArticles(1, id)
|
||||
}
|
||||
|
||||
//ListAllArticles list all articles(without not published)
|
||||
// TODO: need only show the user's not published article
|
||||
func ListAllArticles() *[]map[string]interface{} {
|
||||
article := models.ListField[models.BackArticle]([]map[string]interface{}{{}}, true, "content")
|
||||
if *article == nil {
|
||||
article = &[]map[string]interface{}{}
|
||||
func ListNotPublishedArticlesByUser(id int) *[]models.ListArtile {
|
||||
return listArticles(0, id)
|
||||
}
|
||||
|
||||
func listArticles(isPublish int, createUser int) *[]models.ListArtile {
|
||||
queryStr := "back_article.is_delete = 0 AND is_publish = " + strconv.Itoa(isPublish)
|
||||
if createUser != 0 {
|
||||
queryStr += " AND create_user = " + strconv.Itoa(createUser)
|
||||
}
|
||||
return article
|
||||
var res []models.ListArtile
|
||||
global.Db.Table("back_article").
|
||||
Select("back_user.username, back_article.*").
|
||||
Joins("join back_user on back_article.create_user=back_user.id").
|
||||
Where(queryStr).Find(&res)
|
||||
if res == nil {
|
||||
res = []models.ListArtile{}
|
||||
}
|
||||
return &res
|
||||
}
|
||||
|
||||
//SaveArticle save the articles
|
||||
|
||||
@@ -2,7 +2,6 @@ package investigate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"nCovTrack-Backend/global"
|
||||
"nCovTrack-Backend/models"
|
||||
"nCovTrack-Backend/utils"
|
||||
@@ -16,12 +15,32 @@ func fakerGetRequest(uri string) string {
|
||||
return string(dataStr)
|
||||
}
|
||||
|
||||
func QueryHotelContacts() {
|
||||
func QueryHotelContacts() []models.HotelContactRequest {
|
||||
dataStr := fakerGetRequest("query/contacts/hotel/320581199103182689")
|
||||
var data []models.HotelContactRequest
|
||||
err := json.Unmarshal([]byte(dataStr), &data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(data)
|
||||
return data
|
||||
}
|
||||
|
||||
func QueryRailwayContacts() []models.RailwayContactRequest {
|
||||
dataStr := fakerGetRequest("query/contacts/railway/320581199103182689")
|
||||
var data []models.RailwayContactRequest
|
||||
err := json.Unmarshal([]byte(dataStr), &data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func QueryPatients() []models.PatientRequest {
|
||||
dataStr := fakerGetRequest("query/contacts/railway/320581199103182689")
|
||||
var data []models.PatientRequest
|
||||
err := json.Unmarshal([]byte(dataStr), &data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -48,7 +48,10 @@ func cacheNCovStatistics() {
|
||||
var nCovRes map[string]string
|
||||
json.Unmarshal([]byte(resp), &nCovRes)
|
||||
var nCovResData map[string]interface{}
|
||||
json.Unmarshal([]byte(nCovRes["data"]), &nCovResData)
|
||||
err := json.Unmarshal([]byte(nCovRes["data"]), &nCovResData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !needToRecache(nCovResData) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ func GetAllCityData(sort string) []interface{} {
|
||||
}
|
||||
|
||||
func GetCountryData(child bool) []interface{} {
|
||||
checkCache()
|
||||
if child {
|
||||
return getEntireRedisList(rds_COUNTRY_LEVEL_CHILD_KEY)
|
||||
}
|
||||
@@ -43,6 +44,7 @@ func GetCountryData(child bool) []interface{} {
|
||||
}
|
||||
|
||||
func GetChinaNCovStatistic() models.ChinaData {
|
||||
checkCache()
|
||||
data := models.ChinaData{}
|
||||
json.Unmarshal([]byte(global.Redis.Get(rds_CHINA_ADD_KEY).Val()), &data.ChinaAdd)
|
||||
json.Unmarshal([]byte(global.Redis.Get(rds_CHINA_TOTAL_KEY).Val()), &data.ChinaTotal)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/google/uuid"
|
||||
@@ -18,6 +17,7 @@ const (
|
||||
|
||||
// Login if login success, will return token
|
||||
func Login(user map[string]interface{}) (token string) {
|
||||
// TODO: need to detect is passed or not
|
||||
account := user["account"].(string)
|
||||
var queryMap []map[string]interface{}
|
||||
if strings.Contains(account, "@") {
|
||||
@@ -29,64 +29,73 @@ func Login(user map[string]interface{}) (token string) {
|
||||
if userInfo == nil {
|
||||
return ""
|
||||
}
|
||||
if userInfo["approver"].(int) <= 0 {
|
||||
return ""
|
||||
}
|
||||
if !utils.PasswordCompare(user["password"].(string), userInfo["password"].(string)) {
|
||||
return ""
|
||||
}
|
||||
claims := jwt.MapClaims{
|
||||
"id": userInfo["id"],
|
||||
"username": userInfo["username"],
|
||||
"role": userInfo["role"],
|
||||
"email": userInfo["email"],
|
||||
"region": userInfo["region"],
|
||||
"role": userInfo["role"],
|
||||
}
|
||||
return utils.GenerateToken(claims)
|
||||
}
|
||||
|
||||
// Register user register, user can use account after approved
|
||||
func Register(user map[string]interface{}) {
|
||||
func Register(user map[string]interface{}) bool {
|
||||
user["password"] = utils.PasswordEncrypt(user["password"].(string))
|
||||
userStr, _ := json.Marshal(user)
|
||||
// insert into redis, wait for approve
|
||||
cmd := global.Redis.HMSet(global.REGISTER_REDIS_KEY, map[string]interface{}{user["email"].(string): userStr})
|
||||
if cmd.Err() != nil {
|
||||
panic(cmd.Err())
|
||||
user["approver"] = 0
|
||||
colMap := models.MapJ2c[models.BackUser](user, false)
|
||||
ok, rowsAffected := models.Upsert[models.BackUser](colMap)
|
||||
if !ok || rowsAffected == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ListRegister list the registers in the redis to be approved
|
||||
func ListRegister() *[]map[string]interface{} {
|
||||
applyStrMap := global.Redis.HGetAll(global.REGISTER_REDIS_KEY).Val()
|
||||
var applies []map[string]interface{}
|
||||
for _, v := range applyStrMap {
|
||||
var apply map[string]interface{}
|
||||
_ = json.Unmarshal([]byte(v), &apply)
|
||||
applies = append(applies, apply)
|
||||
func ListRegister(claims models.TokenClaims) *[]map[string]interface{} {
|
||||
registers := []map[string]interface{}{}
|
||||
tx := global.Db.Model(new(models.BackUser)).Omit("password")
|
||||
if claims.Region == "" {
|
||||
// do nothing
|
||||
} else if !strings.Contains(claims.Region, " ") {
|
||||
tx.Where("approver = 0 AND is_delete = 0 AND region LIKE ? AND role = ?", claims.Region+" %", global.ROLE_ID_MAP["ADMIN"])
|
||||
registers = *models.ListByOrm(tx)
|
||||
} else {
|
||||
tx.Where("approver = 0 AND is_delete = 0 AND region = ? AND role in ?", claims.Region, []int{global.ROLE_ID_MAP["WORKER"], global.ROLE_ID_MAP["VOLUNTEER"]})
|
||||
registers = *models.ListByOrm(tx)
|
||||
}
|
||||
if applies == nil {
|
||||
applies = []map[string]interface{}{}
|
||||
}
|
||||
return &applies
|
||||
return ®isters
|
||||
}
|
||||
|
||||
// ListApprovedRegister list registers approved by the admin
|
||||
func ListApprovedRegister(claims models.TokenClaims) *[]map[string]interface{} {
|
||||
approvedRegisters := []map[string]interface{}{}
|
||||
tx := global.Db.Model(new(models.BackUser)).Omit("password").Where("approver in ? and is_delete = 0", []int{claims.ID, -claims.ID})
|
||||
approvedRegisters = *models.ListByOrm(tx)
|
||||
return &approvedRegisters
|
||||
}
|
||||
|
||||
// ApproveRegister approve a register
|
||||
func ApproveRegister(email string, pass bool) bool {
|
||||
if !pass {
|
||||
rowsAffected := global.Redis.HDel(global.REGISTER_REDIS_KEY, email).Val()
|
||||
return rowsAffected != 0
|
||||
func ApproveRegister(claims models.TokenClaims, email string, pass bool) bool {
|
||||
queryMap := []map[string]interface{}{{"email": email}}
|
||||
var approver int
|
||||
if pass {
|
||||
approver = claims.ID
|
||||
} else {
|
||||
approver = -claims.ID
|
||||
}
|
||||
// if pass, will get the register info from redis, and the insert into mysql, this mean user is register success
|
||||
applyStr := global.Redis.HGet(global.REGISTER_REDIS_KEY, email).Val()
|
||||
rowsAffected := global.Redis.HDel(global.REGISTER_REDIS_KEY, email).Val()
|
||||
if rowsAffected == 0 {
|
||||
updateMap := map[string]interface{}{"approver": approver}
|
||||
ok, rowsAffected := models.Update[models.BackUser](queryMap, updateMap)
|
||||
if !ok || rowsAffected == 0 {
|
||||
return false
|
||||
}
|
||||
var apply map[string]interface{}
|
||||
_ = json.Unmarshal([]byte(applyStr), &apply)
|
||||
if !NoDuplicatePhoneOrEmail(apply["phone"].(string), apply["email"].(string)) {
|
||||
return false
|
||||
}
|
||||
colMap := models.MapJ2c[models.BackUser](apply, true)
|
||||
ok, rowsAffected := models.Upsert[models.BackUser](colMap)
|
||||
return ok && rowsAffected != 0
|
||||
return true
|
||||
}
|
||||
|
||||
// ChangePassword user change password, or user forgot password
|
||||
|
||||
Reference in New Issue
Block a user