master
DEV 2 years ago
parent 7094c57353
commit 265e455452

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Active8.iml" filepath="$PROJECT_DIR$/.idea/Active8.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -23,3 +23,12 @@
// ------------------------------------------------------------------------
package brbac
var (
g = struct {
agentAllFlags []AgentStateFlags
}{
agentAllFlags: []AgentStateFlags{AgentStateFlagVerified, AgentStateFlagRequired, AgentStateFlagRestricted, AgentStateFlagLimited},
}
)

@ -2,9 +2,9 @@
// 🚀 Project Active8
// 🔗 Active Thing (activething.com) git.activething.com/go
//
// File name role_id.go
// File name agent_state.go
// ✍️ Created by DEV
// 📅 Modified 06/12/2024
// 📅 Modified 07/12/2024
//
// 🔒 Copyright 2024 activething.com
//
@ -24,4 +24,18 @@
package brbac
type RoleID uint64
const (
AgentStateActive AgentState = 0b000 // El usuario está activo y no tiene restricciones
AgentStateInactive AgentState = 0b001 // El usuario está inactivo y temporalmente deshabilitado
AgentStatePending AgentState = 0b010 // El usuario está pendiente de activación (por ejemplo, verificación de correo electrónico)
AgentStateSuspended AgentState = 0b011 // El usuario está suspendido debido a problemas de seguridad o políticas
AgentStateRevoked AgentState = 0b100 // El acceso del usuario ha sido revocado permanentemente
AgentStatePaused AgentState = 0b101 // El usuario ha pausado su cuenta (puede reactivarse)
AgentStateExpired AgentState = 0b110 // La cuenta del usuario ha expirado (por ejemplo, acceso temporal)
AgentStateDeleted AgentState = 0b111 // La cuenta del usuario está eliminada y ya no existe
)
type (
AgentState uint8
)

@ -0,0 +1,89 @@
// ------------------------------------------------------------------------
// 🚀 Project Active8
// 🔗 Active Thing (activething.com) git.activething.com/go
//
// File name agent_state_flags.go
// ✍️ Created by DEV
// 📅 Modified 07/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 "math/bits"
const (
AgentStateFlagVerified AgentStateFlags = 0b00000001
AgentStateFlagRequired AgentStateFlags = 0b00000010
AgentStateFlagRestricted AgentStateFlags = 0b00000011
AgentStateFlagLimited AgentStateFlags = 0b00000100
)
type (
AgentStateFlags uint8
)
func (a AgentStateFlags) Has(flag AgentStateFlags) bool {
return a&flag != 0
}
func (a AgentStateFlags) HasVerified() bool {
return a&AgentStateFlagVerified != 0
}
func (a AgentStateFlags) HasRequired() bool {
return a&AgentStateFlagRequired != 0
}
func (a AgentStateFlags) HasRestricted() bool {
return a&AgentStateFlagRestricted != 0
}
func (a AgentStateFlags) HasLimited() bool {
return a&AgentStateFlagLimited != 0
}
func (a AgentStateFlags) Count() int {
if a == 0 {
return 0
}
return bits.OnesCount8(uint8(a))
}
func (a AgentStateFlags) Actives() []AgentStateFlags {
var l []AgentStateFlags
if a > 0 {
for _, f := range g.agentAllFlags {
if a&f != 0 {
l = append(l, f)
}
}
}
return l
}
func (a AgentStateFlags) Set(flag AgentStateFlags) AgentStateFlags {
return a | flag
}
func (a AgentStateFlags) Del(flag AgentStateFlags) AgentStateFlags {
return a &^ flag
}
func (a AgentStateFlags) Clear() AgentStateFlags {
return 0
}

@ -23,89 +23,3 @@
// ------------------------------------------------------------------------
package tests
import (
"brbac"
"testing"
)
func TestUserID(t *testing.T) {
// Test creating a new User
source := byte(42)
state := byte(7)
id := uint64(0x123456789ABC)
uid := brbac.NewUserID(source, state, id)
// Verify source
if got := uid.Source(); got != source {
t.Errorf("Group: expected %d, got %d", source, got)
}
// Verify state
if got := uid.State(); got != state {
t.Errorf("State: expected %d, got %d", state, got)
}
// Verify ID
if got := uid.ID(); got != id {
t.Errorf("ID: expected 0x%X, got 0x%X", id, got)
}
}
func TestUserIDSetters(t *testing.T) {
var uid brbac.User
// Test SetGroup
source := byte(55)
uid = uid.SetGroup(source)
if got := uid.Group(); got != source {
t.Errorf("SetGroup: expected %d, got %d", source, got)
}
// Test SetState
state := byte(22)
uid = uid.SetState(state)
if got := uid.State(); got != state {
t.Errorf("SetState: expected %d, got %d", state, got)
}
// Test SetID
id := uint64(0xFFFFFFFFFFFF)
uid = uid.SetID(id)
if got := uid.ID(); got != id {
t.Errorf("SetID: expected 0x%X, got 0x%X", id, got)
}
}
func TestUserIDChaining(t *testing.T) {
source := byte(33)
state := byte(11)
id := uint64(0x123456789ABC)
uid := brbac.NewUserID(source, state, id)
// Verify all components after chained creation
if got := uid.Source(); got != source {
t.Errorf("Chained Group: expected %d, got %d", source, got)
}
if got := uid.State(); got != state {
t.Errorf("Chained State: expected %d, got %d", state, got)
}
if got := uid.ID(); got != id {
t.Errorf("Chained ID: expected 0x%X, got 0x%X", id, got)
}
}
func TestUserIDOverflow(t *testing.T) {
// Test that ID is truncated to 48 bits
fullID := uint64(0xFFFFFFFFFFFFFFFF)
uid := brbac.User(0).SetID(fullID)
expectedID := fullID & brbac.userIdMask
if got := uid.ID(); got != expectedID {
t.Errorf("ID Overflow: expected 0x%X, got 0x%X", expectedID, got)
}
}

@ -1,58 +0,0 @@
// ------------------------------------------------------------------------
// 🚀 Project Active8
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
//
// File name user.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
const (
userGpMask uint64 = 0xFF << 56
userIdMask uint64 = 0x00FFFFFFFFFFFFFF
)
type (
User uint64
)
// NewUser creates a new User with optional components
func NewUser(groupID byte, id uint64) User {
var uid User
return uid.SetGroup(groupID) | uid.SetID(id)
}
func (u User) ID() uint64 { return uint64(u) & userIdMask }
func (u User) Group() byte { return byte((uint64(u) & userGpMask) >> 48) }
// SetGroup updates the group bits of the User
func (u User) SetGroup(groupId byte) User {
return User((uint64(u) & ^userGpMask) | (uint64(groupId) << 48))
}
// SetID updates the 48-bit identifier of the User
func (u User) SetID(id uint64) User {
if id > userIdMask {
//todo
panic("Excced max id")
}
return User((uint64(u) & ^userIdMask) | (id & userIdMask))
}

@ -1,121 +0,0 @@
// ------------------------------------------------------------------------
// 🚀 Project Active8
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
//
// File name user_group_countries.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
var (
codesCountries = [256]string{
"",
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS",
"BT", "BV", "BW", "BY", "BZ",
"CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW",
"CX", "CY", "CZ",
"DE", "DJ", "DK", "DM", "DO", "DZ",
"EC", "EE", "EG", "EH", "ER", "ES", "ET",
"FI", "FJ", "FK", "FM", "FO", "FR",
"GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
"GU", "GW", "GY",
"HK", "HM", "HN", "HR", "HT", "HU",
"ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT",
"JE", "JM", "JO", "JP", "KE",
"KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ",
"LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY",
"MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ",
"MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ",
"NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ",
"OM",
"PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PT", "PW", "PY",
"QA",
"RE", "RO", "RS", "RU", "RW",
"SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO",
"SR", "SS", "ST", "SV", "SX", "SY", "SZ",
"TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV",
"TW", "TZ",
"UA", "UG", "UM", "US", "UY", "UZ",
"VA", "VC", "VE", "VG", "VI", "VN", "VU",
"WF", "WS",
"YE", "YT",
"ZA", "ZM", "ZW",
}
countriesCodes = fillCountriesCode()
)
func fillCountriesCode() map[string]byte {
m := make(map[string]byte, len(codesCountries))
for i := 0; i < len(codesCountries); i++ {
m[codesCountries[i]] = byte(i)
}
return m
}
type (
UserSourceCountries struct{}
)
// GetAlpha Obtener el código alfabético de un país usando el índice
func (_ UserSourceCountries) GetAlpha(code byte) (string, bool) {
// Verificar que el código esté dentro del rango válido
c := int(code)
if c >= 1 && c < len(codesCountries) {
return codesCountries[c], true
}
// Si el código no es válido, retornar un error
return "", false
}
// GetCode Obtener el código numérico del país usando el código alfabético
func (_ UserSourceCountries) GetCode(alpha string) byte {
// Verificar si el código alfabético existe en el mapa
if code, exists := countriesCodes[alpha]; exists {
return code
}
// Si el código no existe, retornar 0 o algún valor que indique error
return 0
}
// ProvideUserSource Proveer el código de usuario basado en un código de país (string o byte)
func (_ UserSourceCountries) ProvideUserSource(code any) byte {
switch v := code.(type) {
case string:
// Si el código es un string (alfabético), obtenemos el código numérico
if c, exists := countriesCodes[v]; exists {
return c
}
//todo
panic("invalid country code")
case byte:
vl := int(v)
// Si el código es un byte, aseguramos que esté en el rango válido
if vl >= 1 && vl < len(codesCountries) {
return v
}
//todo
panic("invalid country code")
default:
//todo
panic("invalid code type")
}
}

@ -1,31 +0,0 @@
// ------------------------------------------------------------------------
// 🚀 Project Active8
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
//
// File name user_group_provider.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
type (
UserSourceProvider interface {
ProvideUserSource(code any) byte
}
)

@ -1,27 +0,0 @@
// ------------------------------------------------------------------------
// 🚀 Project Active8
// 🔗 Active Thing (activething.com) git.activething.com/go
//
// File name user_id.go
// ✍️ Created by DEV
// 📅 Modified 06/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
type UserID uint64

@ -1,114 +0,0 @@
// ------------------------------------------------------------------------
// 🚀 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
}
Loading…
Cancel
Save

Powered by TurnKey Linux.