| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 | /* * @description: 访问权限认证 * @Author: CP * @Date: 2020-10-09 10:43:39 * @FilePath: \construction_management\web\middleware\accessAuth.go */package middlewareimport (	"errors"	"github.com/kataras/iris/v12")// 员工表权限解析type permission struct {	Add    int ` json:"add" `	Delete int ` json:"delete" `	Access int ` json:"access" `}// 权限JSON对象type permissionPath struct {	Contract       path     `json:"contract" `	Safe           path     `json:"safe" `	Quality        path     `json:"quality" `	ProjectSetting []string `json:"projectSetting" `}// 对象中地址列表type path struct {	Add    []string `json:"add" `	Access []string `json:"access" `	Delete []string `json:"delete" `}// 权限验证中间件func AccessAuth(ctx iris.Context) {	// // 1.获得成员信息	// account := ctx.Values().Get("account").(*models.CmProjectAccount)	// // 1-1.是管理员- 拥有所有权限	// if account.IsAdmin != 1 {	// 	// if account.IsAdmin == 1 {	// 	// 2.获得员工可访问的权限	// 	contractPermission := permission{}	// 	if account.ContractPermission != "" {	// 		err := json.Unmarshal([]byte(account.ContractPermission), &contractPermission)	// 		// 错误后 全部权限默认为0	// 		if err != nil {	// 			log.Println("合同权限解析错误:err=", err)	// 		}	// 	}	// 	safePermission := permission{}	// 	if account.SafePermission != "" {	// 		err := json.Unmarshal([]byte(account.SafePermission), &safePermission)	// 		if err != nil {	// 			log.Println("安全权限解析错误:err=", err)	// 		}	// 	}	// 	qualityPermission := permission{}	// 	if account.QualityPermission != "" {	// 		err := json.Unmarshal([]byte(account.QualityPermission), &qualityPermission)	// 		if err != nil {	// 			log.Println("质量权限解析错误:err=", err)	// 		}	// 	}	// 	// 2-1获得权限列表	// 	permissionPath := permissionPath{}	// 	permissionPathData, err := ioutil.ReadFile("../lib/permission.json")	// 	err = json.Unmarshal(permissionPathData, &permissionPath)	// 	if err != nil {	// 		log.Println("权限解析错误:err=", err)	// 		ctx.JSON(iris.Map{"code": 2, "msg": "权限解析错误"})	// 		return	// 	}	// 	// 2-2 不容许访问的权限--比对访问路径	// 	requestPath := ctx.Path()	// 	// 合同权限	// 	err = verifyAuth(contractPermission, permissionPath.Contract, requestPath)	// 	if err != nil {	// 		ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})	// 		return	// 	}	// 	// 安全权限	// 	err = verifyAuth(contractPermission, permissionPath.Safe, requestPath)	// 	if err != nil {	// 		ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})	// 		return	// 	}	// 	// 质量权限	// 	err = verifyAuth(contractPermission, permissionPath.Quality, requestPath)	// 	if err != nil {	// 		ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})	// 		return	// 	}	// 	// 项目设置 -只有管理员才能访问	// 	for _, path := range permissionPath.ProjectSetting {	// 		if path == requestPath {	// 			ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})	// 			return	// 		}	// 	}	// }	ctx.Next()}// 验证权限路径func verifyAuth(permission permission, pathList path, requestPath string) error {	if permission.Add == 0 {		for _, path := range pathList.Add {			if path == requestPath {				return errors.New("无权访问")			}		}	}	if permission.Access == 0 {		for _, path := range pathList.Access {			if path == requestPath {				return errors.New("无权访问")			}		}	}	if permission.Delete == 0 {		for _, path := range pathList.Delete {			if path == requestPath {				return errors.New("无权访问")			}		}	}	return nil}
 |