commit
7094c57353
@ -0,0 +1,25 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project Active8
|
||||
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.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
|
||||
@ -0,0 +1,3 @@
|
||||
module brbac
|
||||
|
||||
go 1.22
|
||||
@ -0,0 +1,27 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project Active8
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name role_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 RoleID uint64
|
||||
@ -0,0 +1,3 @@
|
||||
module tests
|
||||
|
||||
go 1.22
|
||||
@ -0,0 +1,111 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project Active8
|
||||
// 🔗 UserStateActive Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name user_test.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 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)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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))
|
||||
}
|
||||
@ -0,0 +1,121 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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
|
||||
}
|
||||
)
|
||||
@ -0,0 +1,27 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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
|
||||
@ -0,0 +1,114 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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…
Reference in new issue