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.
115 lines
4.3 KiB
115 lines
4.3 KiB
// ------------------------------------------------------------------------
|
|
// 🚀 Project Active8
|
|
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name user_state.go
|
|
// ✍️ Created by DEV
|
|
// 📅 Modified 04/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
|
|
|
|
// Estados (4 bits para los estados)
|
|
const (
|
|
UserStateActive UserState = 0b0000 // El usuario está activo y no tiene restricciones
|
|
UserStateInactive UserState = 0b0001 // El usuario está inactivo y temporalmente deshabilitado
|
|
UserStatePending UserState = 0b0010 // El usuario está pendiente de activación (por ejemplo, verificación de correo electrónico)
|
|
UserStateSuspended UserState = 0b0011 // El usuario está suspendido debido a problemas de seguridad o políticas
|
|
UserStateRevoked UserState = 0b0100 // El acceso del usuario ha sido revocado permanentemente
|
|
UserStateExpired UserState = 0b0101 // La cuenta del usuario ha expirado (por ejemplo, acceso temporal)
|
|
UserStatePaused UserState = 0b0110 // El usuario ha pausado su cuenta (puede reactivarse)
|
|
UserStateDeleted UserState = 0b0111 // La cuenta del usuario está eliminada y ya no existe
|
|
|
|
)
|
|
|
|
// Flags (4 bits para los flags)
|
|
const (
|
|
UserFlagRestricted UserState = 0b00010000 // El acceso del usuario está limitado (restringido a ciertos recursos)
|
|
UserFlagSupervised UserState = 0b10000000 // El usuario esta supervisado
|
|
)
|
|
|
|
type UserState byte
|
|
|
|
func (s UserState) Set(newState UserState, flags ...UserState) UserState {
|
|
if len(flags) == 0 {
|
|
return (s & 0b11110000) | (newState & 0b1111)
|
|
}
|
|
return (s & 0b11110000) | (newState & 0b1111) | (s & 0b00001111) | (flags[0] & 0b11110000)
|
|
}
|
|
|
|
// SetState Establecer el estado sin afectar los flags
|
|
func (s UserState) SetState(newState UserState) UserState {
|
|
// Usamos una máscara para preservar los flags y solo cambiar el estado
|
|
return (s & 0b11110000) | (newState & 0b1111)
|
|
}
|
|
|
|
// SetFlags Establecer los flags sin afectar el estado
|
|
func (s UserState) SetFlags(newFlags UserState) UserState {
|
|
// Usamos una máscara para preservar el estado y solo cambiar los flags
|
|
return (s & 0b00001111) | (newFlags & 0b11110000)
|
|
}
|
|
|
|
// IsActive verifica si el usuario está activo, ignorando los flags
|
|
func (s UserState) IsActive() bool {
|
|
// Compara solo los primeros 4 bits (estado) con el valor de UserStateActive
|
|
return (s & 0b1111) == UserStateActive
|
|
}
|
|
|
|
// IsInactive verifica si el usuario está inactivo, ignorando los flags
|
|
func (s UserState) IsInactive() bool {
|
|
return (s & 0b1111) == UserStateInactive
|
|
}
|
|
|
|
// IsPending verifica si el usuario está pendiente de activación, ignorando los flags
|
|
func (s UserState) IsPending() bool {
|
|
return (s & 0b1111) == UserStatePending
|
|
}
|
|
|
|
// IsSuspended verifica si el usuario está suspendido, ignorando los flags
|
|
func (s UserState) IsSuspended() bool {
|
|
return (s & 0b1111) == UserStateSuspended
|
|
}
|
|
|
|
// IsRevoked verifica si el acceso del usuario ha sido revocado, ignorando los flags
|
|
func (s UserState) IsRevoked() bool {
|
|
return (s & 0b1111) == UserStateRevoked
|
|
}
|
|
|
|
// IsPaused verifica si el usuario ha pausado su cuenta, ignorando los flags
|
|
func (s UserState) IsPaused() bool {
|
|
return (s & 0b1111) == UserStatePaused
|
|
}
|
|
|
|
// IsExpired verifica si la cuenta del usuario ha expirado, ignorando los flags
|
|
func (s UserState) IsExpired(state UserState) bool {
|
|
return (s & 0b1111) == UserStateExpired
|
|
}
|
|
|
|
// IsDeleted verifica si la cuenta del usuario ha sido eliminada, ignorando los flags
|
|
func (s UserState) IsDeleted() bool {
|
|
return (s & 0b1111) == UserStateDeleted
|
|
}
|
|
|
|
func (s UserState) HasSupervision() bool {
|
|
return s&UserFlagRestricted != 0
|
|
}
|
|
|
|
func (s UserState) HasRestriction() bool {
|
|
return s&UserFlagSupervised != 0
|
|
}
|