Example #1
0
func (e *engine) Decision(roleName string, res string, perms ...string) bool {
	rootId, _, exist := e.GetRole(roleName, false)
	if !exist {
		return false
	}
	for _, p := range perms {
		pid, exist := e.GetPerm(p, res, false)
		if !exist {
			return false
		}
		f := func(rid int) bool {
			if pms, ok := e.rolePerm[rid]; !ok {
				return false
			} else if i := pms.Search(pid); i < len(pms) && pms[i] == pid {
				return true
			}
			return false
		}
		found := e.searchRoleGraph(rootId, f)
		if !found {
			return false
		}
	}
	return true
}
Example #2
0
func (e *engine) DecisionEx(roleName string, res string, perms ...string) bool {
	rootId, _, exist := e.GetRole(roleName, false)
	if !exist {
		return false
	}
	r1, err := resource.Parse(res)
	if err != nil {
		panic(err)
	}
	for _, permName := range perms {
		f := func(rid int) bool {
			for _, pid := range e.rolePerm[rid] {
				if e.storage[pid].sName != permName {
					continue
				} else if r2, err := resource.Parse(e.storage[pid].sContent); err != nil {
					panic(err)
				} else if r2.Contains(r1) {
					return true
				}
			}
			return false
		}
		found := e.searchRoleGraph(rootId, f)
		if !found {
			return false
		}
	}
	return true
}
Example #3
0
func (e *engine) HasAnyRole(roleName string, hasRoleNames ...string) bool {
	rootId, _, exist := e.GetRole(roleName, false)
	if !exist {
		return false
	}
	for _, r := range hasRoleNames {
		id, _, exist := e.GetRole(r, false)
		if !exist {
			continue
		}
		f := func(nid int) bool {
			return nid == id
		}
		found := e.searchRoleGraph(rootId, f)
		if found {
			return true
		}
	}
	return false
}