feat:完善项目信息

This commit is contained in:
henry
2022-01-07 16:12:43 +08:00
parent 657fdc5750
commit 2bf3c01702
21 changed files with 379 additions and 129 deletions

View File

@ -168,8 +168,28 @@ func (this *Permission) AddPolicies() (bool, error) {
return auth.enforcer.AddPolicies(rules)
}
// RemoveRolePolicy 删除角色的所有规则
func (this *Permission) RemoveRolePolicy() (bool, error) {
// RemoveRolePolicies 删除角色指定规则
func (this *Permission) RemoveRolePolicies() (bool, error) {
if len(this.roles) <= 0 {
return false, errors.New("not role")
}
rules := make([][]string, 0)
tenantFormat := this.tenantFormat()
for _, v := range this.roleFormat() {
for _, request := range this.request {
rules = append(rules, []string{
v, tenantFormat, request.Url, request.Method,
})
}
}
return auth.enforcer.RemovePolicies(rules)
}
// RemoveSingleRolePolicy 删除角色的所有规则
func (this *Permission) RemoveSingleRolePolicy() (bool, error) {
if len(this.roles) <= 0 {
return false, errors.New("not role")
}

47
app/service/auth_test.go Normal file
View File

@ -0,0 +1,47 @@
package service
import (
"SciencesServer/app/common/model"
"SciencesServer/serve/orm"
"SciencesServer/serve/orm/logic"
"gorm.io/gorm"
"testing"
)
func mysql() *gorm.DB {
instance := orm.NewInstance(
orm.WithDebug(false),
orm.WithDBMode("mysql"),
orm.WithTablePrefix(""),
orm.WithSingularTable(false),
orm.WithMaxIdleConns(3600),
orm.WithMaxOpenConns(2000),
orm.WithMaxLifetime(1000),
orm.WithMysqlOption(&logic.Mysql{
Username: "appuser", Password: "ABCabc01", Host: "192.168.0.188", Port: 3306,
Database: "sciences", Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
}),
).Init()
return instance.Engine
}
func TestNewPermission(t *testing.T) {
mysql := mysql()
NewAuth().Register()("mysql", mysql, model.NewSysAuthRule().TableName())
permission := NewPermission(
WithAuthTenant("0"),
WithAuthUser("123"),
WithAuthRoles([]string{"1", "2"}),
WithAuthRequest([]*AuthRequest{&AuthRequest{
Url: "/admin/123",
Method: "*",
}}),
)
//status, err := permission.AddPolicies()
//status, err := permission.AddRoleForUser()
//status, err := permission.RemoveRolePolicies()
status, err := permission.Enforce()
t.Log(status)
t.Log(err)
}