group_manager.go 1.77 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
package group_s

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"hilo-group/_const/enum/country_e"
	"hilo-group/domain/model/country_m"
	"hilo-group/domain/model/user_m"
	"hilo-group/myerr/bizerr"
)

// 检查是否App管理员
// 1. 全球广播管理员 gm
// 2. 国家管理员 cm
func (s *GroupService) CheckAppManager(userId mysql.ID) (gm bool, cm bool, err error) {
	var n int64
	if err := mysql.Db.Model(&user_m.GlobalBroadcastManager{}).Where(&user_m.GlobalBroadcastManager{UserId: userId}).Count(&n).Error; err != nil {
		return false, false, err
	}
	if n > 0 {
		gm = true
	}
	var _model = domain.CreateModel(s.svc.CtxAndDb)
	countryManager, err := country_m.GetCountryMgr(_model, userId)
	if err != nil {
		return false, false, err
	}
	if countryManager != nil && countryManager.Role == country_e.CountryMgrManager {
		cm = true
	}
	return
}

// 检查是否有重置/删除权限
// conditions
//  1. userId是国家管理员
//  2. userId和resetUserId是同国
//  3. resetUserId财富等级小于5级
// return
//  true: 有权限
//  err: 无权限的err
func (s *GroupService) CheckCountryManagerPermission(userId, resetUserId mysql.ID) (bool, error) {
	var _model = domain.CreateModel(s.svc.CtxAndDb)
	countryManager, err := country_m.GetCountryMgr(_model, userId)
	if err != nil {
		return false, err
	}
	if countryManager == nil || countryManager.Role != country_e.CountryMgrManager {
		return false, bizerr.ManagerNoPermission
	}
	user, err := user_m.GetUser(_model, resetUserId)
	if err != nil {
		return false, err
	}
	grade, _, err := user_m.GetWealthGrade(_model, resetUserId)
	if err != nil {
		return false, err
	}
	if grade >= 5 || countryManager.Country != user.Country {
		return false, bizerr.ManagerNoUserPermission
	}
	return true, nil
}