diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/Active8.iml b/.idea/Active8.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/Active8.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..00e578b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/brbac/active.go b/brbac/active.go index 3beb0f4..7160aa5 100644 --- a/brbac/active.go +++ b/brbac/active.go @@ -23,3 +23,12 @@ // ------------------------------------------------------------------------ package brbac + +var ( + g = struct { + agentAllFlags []AgentStateFlags + }{ + + agentAllFlags: []AgentStateFlags{AgentStateFlagVerified, AgentStateFlagRequired, AgentStateFlagRestricted, AgentStateFlagLimited}, + } +) diff --git a/brbac/role_id.go b/brbac/agent_state.go similarity index 50% rename from brbac/role_id.go rename to brbac/agent_state.go index 8596a18..c0a359c 100644 --- a/brbac/role_id.go +++ b/brbac/agent_state.go @@ -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 +) diff --git a/brbac/agent_state_flags.go b/brbac/agent_state_flags.go new file mode 100644 index 0000000..e85cbe6 --- /dev/null +++ b/brbac/agent_state_flags.go @@ -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 +} diff --git a/brbac/tests/user_test.go b/brbac/tests/user_test.go index 5d087f5..5a99975 100644 --- a/brbac/tests/user_test.go +++ b/brbac/tests/user_test.go @@ -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) - } -} diff --git a/brbac/user.go b/brbac/user.go deleted file mode 100644 index c0952e1..0000000 --- a/brbac/user.go +++ /dev/null @@ -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)) -} diff --git a/brbac/user_group_countries.go b/brbac/user_group_countries.go deleted file mode 100644 index 1daf081..0000000 --- a/brbac/user_group_countries.go +++ /dev/null @@ -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") - } -} diff --git a/brbac/user_group_provider.go b/brbac/user_group_provider.go deleted file mode 100644 index 0256562..0000000 --- a/brbac/user_group_provider.go +++ /dev/null @@ -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 - } -) diff --git a/brbac/user_id.go b/brbac/user_id.go deleted file mode 100644 index f4bd49d..0000000 --- a/brbac/user_id.go +++ /dev/null @@ -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 diff --git a/brbac/user_state.go b/brbac/user_state.go deleted file mode 100644 index 20df5de..0000000 --- a/brbac/user_state.go +++ /dev/null @@ -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 -}