You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.6 KiB
96 lines
2.6 KiB
// ------------------------------------------------------------------------
|
|
// 🚀 Project Active8
|
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name role_manager.go
|
|
// ✍️ Created by DEV
|
|
// 📅 Modified 09/12/2024
|
|
//
|
|
// 🔒 Copyright 2024 activething.com
|
|
//
|
|
// 📜 LICENSE --------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package brbac
|
|
|
|
import "rsls"
|
|
|
|
type (
|
|
|
|
|
|
RoleManager interface {
|
|
|
|
Create () (*Role,error)
|
|
Update (role *Role) error
|
|
Delete (roleID ActorID) error
|
|
|
|
DelSubRole (roleID ...ActorID) error
|
|
AddSubRole (roleID ...ActorID) error
|
|
}
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func DetectRoleCycle(roleID, newRoleID ActorID, provider RoleProvider) rsls.RBool {
|
|
state := make(map[ActorID]byte)
|
|
return detectRoleCycle(roleID, newRoleID, provider, &state)
|
|
}
|
|
|
|
|
|
|
|
|
|
// detectRoleCycle realiza la verificación recursiva de si se forma un ciclo
|
|
func detectRoleCycle(roleID, newRoleID ActorID, provider RoleProvider, state *map[ActorID]byte) rsls.RBool {
|
|
|
|
// Si el nuevo rol ya está en la jerarquía del rol actual, entonces hemos encontrado un ciclo
|
|
if (*state)[newRoleID] == 1 {
|
|
return rsls.RBoolOkTrue // Ciclo detectado
|
|
}
|
|
|
|
// Si el rol ha sido visitado completamente, entonces no es necesario revisarlo de nuevo
|
|
if (*state)[newRoleID] == 2 {
|
|
return rsls.RBoolOkFalse
|
|
}
|
|
|
|
// Marcar el rol como "en proceso"
|
|
(*state)[newRoleID] = 1
|
|
|
|
// Obtener los subroles del nuevo rol
|
|
subRoles, err := provider.GetSubRoles(newRoleID)
|
|
if err != nil {
|
|
return rsls.MakeBoolError(err) // Error al obtener los subroles
|
|
}
|
|
|
|
// Realizar DFS para cada subrol del nuevo rol
|
|
for _, subRoleID := range subRoles {
|
|
if subRoleID == roleID {
|
|
// Detectar referencia directa al rol original, indicando un ciclo
|
|
return rsls.RBoolOkTrue
|
|
}
|
|
cycleCheck := detectRoleCycle(roleID, subRoleID, provider, state)
|
|
if !cycleCheck.IsOk() ||cycleCheck.Result() {
|
|
return cycleCheck
|
|
}
|
|
}
|
|
|
|
// Marcar el rol como "visitado" después de procesar todos sus subroles
|
|
(*state)[newRoleID] = 2
|
|
return rsls.RBoolOkFalse // No se ha detectado un ciclo
|
|
}
|
|
|
|
|
|
|