Skip to content

Commit bee5dae

Browse files
committed
Add ACE principals API
1 parent bdf02d2 commit bee5dae

8 files changed

Lines changed: 293 additions & 12 deletions

File tree

docs/api/data.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,44 @@ User ID, or a player with the given Data ID is currently connected.
161161

162162
- `boolean` - Whether the migration was successful.
163163

164+
### Add Data ID Principal
165+
166+
```
167+
exports.d4_playerdata:addDataIdPrincipal(dataId, principal)
168+
```
169+
170+
Persists a given ACE principal to a given Data ID and returns whether it was
171+
successful.
172+
Returns `false` if the principal already exists for the given Data ID.
173+
174+
#### Parameters
175+
176+
- `dataId: integer` - The Data ID.
177+
- `principal: string` - The ACE Principal.
178+
179+
#### Returns
180+
181+
- `boolean` - Whether the principal was added.
182+
183+
### Remove Data ID Principal
184+
185+
```
186+
exports.d4_playerdata:removeDataIdPrincipal(dataId, principal)
187+
```
188+
189+
Removes a given ACE principal from a given Data ID and returns whether it was
190+
successful.
191+
Returns `false` if the principal does not exist for the given Data ID.
192+
193+
#### Parameters
194+
195+
- `dataId: integer` - The Data ID.
196+
- `principal: string` - The ACE Principal.
197+
198+
#### Returns
199+
200+
- `boolean` - Whether the principal was removed.
201+
164202
### Get User ID from Data ID
165203

166204
```

docs/api/users.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,44 @@ Data ID.
129129

130130
- `boolean` - Whether the migration was successful.
131131

132+
### Add User ID Principal
133+
134+
```
135+
exports.d4_playerdata:addUserIdPrincipal(userId, principal)
136+
```
137+
138+
Persists a given ACE principal to a given User ID and returns whether it was
139+
successful.
140+
Returns `false` if the principal already exists for the given User ID.
141+
142+
#### Parameters
143+
144+
- `userId: integer` - The User ID.
145+
- `principal: string` - The ACE Principal.
146+
147+
#### Returns
148+
149+
- `boolean` - Whether the principal was added.
150+
151+
### Remove User ID Principal
152+
153+
```
154+
exports.d4_playerdata:removeUserIdPrincipal(userId, principal)
155+
```
156+
157+
Removes a given ACE principal from a given User ID and returns whether it was
158+
successful.
159+
Returns `false` if the principal does not exist for the given User ID.
160+
161+
#### Parameters
162+
163+
- `userId: integer` - The User ID.
164+
- `principal: string` - The ACE Principal.
165+
166+
#### Returns
167+
168+
- `boolean` - Whether the principal was removed.
169+
132170
### Get User ID from Identifier
133171

134172
```

server/api/data.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ function API.data.assign(player, dataId)
4646
playerToDataId[player] = dataId
4747
dataIdToPlayer[dataId] = player
4848

49+
local principals = Storage.dataToPrincipal.getAll(dataId)
50+
for i = 1, #principals do
51+
Utils.addDataPrincipal(dataId, principals[i])
52+
end
4953
Utils.addPlayerPrincipal(player, Utils.getDataAceName(dataId))
54+
5055
if resource == nil then
5156
print(('Player %s (User ID %s) was auto-assigned Data ID %s')
5257
:format(player, userId, dataId))
@@ -72,7 +77,13 @@ function API.data.unassign(player)
7277

7378
playerToDataId[source] = nil
7479
dataIdToPlayer[dataId] = nil
80+
7581
Utils.removePlayerPrincipal(player, Utils.getDataAceName(dataId))
82+
local principals = Storage.dataToPrincipal.getAll(dataId)
83+
for i = 1, #principals do
84+
Utils.removeDataPrincipal(dataId, principals[i])
85+
end
86+
7687
print(('Player %s (User ID %s) was unassigned Data ID %s')
7788
:format(player, API.users.get(player), dataId))
7889
TriggerEvent('d4_playerdata:dataUnassigned', player, dataId)
@@ -143,6 +154,42 @@ function API.data.migrate(dataId, newUserId)
143154
return true
144155
end
145156

157+
---@param dataId integer
158+
---@param principal string
159+
---@return boolean success
160+
function API.data.addPrincipal(dataId, principal)
161+
if Storage.dataToPrincipal.exists(dataId, principal) then
162+
return false
163+
end
164+
165+
Storage.dataToPrincipal.set(dataId, principal)
166+
FlushResourceKvp()
167+
168+
if API.data.getPlayer(dataId) ~= nil then
169+
Utils.addDataPrincipal(dataId, principal)
170+
end
171+
172+
return true
173+
end
174+
175+
---@param dataId integer
176+
---@param principal string
177+
---@return boolean success
178+
function API.data.removePrincipal(dataId, principal)
179+
if not Storage.dataToPrincipal.exists(dataId, principal) then
180+
return false
181+
end
182+
183+
Storage.dataToPrincipal.delete(dataId, principal)
184+
FlushResourceKvp()
185+
186+
if API.data.getPlayer(dataId) ~= nil then
187+
Utils.removeUserPrincipal(dataId, principal)
188+
end
189+
190+
return true
191+
end
192+
146193
---@param player unknown
147194
---@return integer? dataId
148195
function API.data.autoAssign(player)
@@ -197,3 +244,5 @@ exports('unassignDataId', API.data.unassign)
197244
exports('createDataId', API.data.create)
198245
exports('deleteDataId', API.data.delete)
199246
exports('migrateDataId', API.data.migrate)
247+
exports('addDataIdPrincipal', API.data.addPrincipal)
248+
exports('removeDataIdPrincipal', API.data.removePrincipal)

server/api/persist.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ local function migrate(oldPersistId, newPersistId)
111111
end
112112

113113
---@param player unknown
114-
---@param createMissing boolean
114+
---@param connecting boolean?
115115
---@return integer? persistId
116-
function API.persist.ensure(player, createMissing)
116+
function API.persist.ensure(player, connecting)
117117
player = tostring(player)
118118

119119
local tokens = Utils.getTokens(player)
@@ -128,7 +128,7 @@ function API.persist.ensure(player, createMissing)
128128

129129
local persistId = persistIds[1]
130130
if persistId == nil then
131-
if not createMissing then
131+
if connecting then
132132
return nil
133133
end
134134

@@ -145,13 +145,15 @@ function API.persist.ensure(player, createMissing)
145145
API.persist.linkUser(persistId, userIds[i])
146146
end
147147

148-
playerToPersistId[player] = persistId
149-
if persistIdToPlayers[persistId] == nil then
150-
persistIdToPlayers[persistId] = {}
151-
end
152-
persistIdToPlayers[persistId][player] = true
148+
if not connecting then
149+
playerToPersistId[player] = persistId
150+
if persistIdToPlayers[persistId] == nil then
151+
persistIdToPlayers[persistId] = {}
152+
end
153+
persistIdToPlayers[persistId][player] = true
153154

154-
print(('Player %s was assigned Persist ID %s'):format(player, persistId))
155+
print(('Player %s was assigned Persist ID %s'):format(player, persistId))
156+
end
155157
FlushResourceKvp()
156158

157159
return persistId

server/api/users.lua

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ function API.users.migrate(oldUserId, newUserId)
132132
return true
133133
end
134134

135+
---@param userId integer
136+
---@param principal string
137+
---@return boolean success
138+
function API.users.addPrincipal(userId, principal)
139+
if Storage.userToPrincipal.exists(userId, principal) then
140+
return false
141+
end
142+
143+
Storage.userToPrincipal.set(userId, principal)
144+
FlushResourceKvp()
145+
146+
if API.users.isConnected(userId) then
147+
Utils.addUserPrincipal(userId, principal)
148+
end
149+
150+
return true
151+
end
152+
153+
---@param userId integer
154+
---@param principal string
155+
---@return boolean success
156+
function API.users.removePrincipal(userId, principal)
157+
if not Storage.userToPrincipal.exists(userId, principal) then
158+
return false
159+
end
160+
161+
Storage.userToPrincipal.delete(userId, principal)
162+
FlushResourceKvp()
163+
164+
if API.users.isConnected(userId) then
165+
Utils.removeUserPrincipal(userId, principal)
166+
end
167+
168+
return true
169+
end
170+
135171
---@param player unknown
136172
---@return integer? userId
137173
function API.users.ensure(player)
@@ -183,10 +219,15 @@ function API.users.ensure(player)
183219
end
184220
end
185221

222+
local principals = Storage.userToPrincipal.getAll(userId)
223+
for i = 1, #principals do
224+
Utils.addUserPrincipal(userId, principals[i])
225+
end
186226
Utils.addPlayerPrincipal(player, Utils.getUserAceName(userId))
187-
print(('Player %s was assigned User ID %s'):format(player, userId))
188227

228+
print(('Player %s was assigned User ID %s'):format(player, userId))
189229
FlushResourceKvp()
230+
190231
if created then
191232
TriggerEvent('d4_playerdata:userCreated', userId)
192233
end
@@ -211,6 +252,11 @@ function API.users.remove(player)
211252
end
212253

213254
Utils.removePlayerPrincipal(player, Utils.getUserAceName(userId))
255+
local principals = Storage.userToPrincipal.getAll(userId)
256+
for i = 1, #principals do
257+
Utils.removeUserPrincipal(userId, principals[i])
258+
end
259+
214260
print(('Player %s was unassigned User ID %s'):format(player, userId))
215261
TriggerEvent('d4_playerdata:userLeft', player, userId)
216262
end
@@ -222,3 +268,5 @@ exports('isUserIdConnected', API.users.isConnected)
222268
exports('getPlayersFromUserId', API.users.getPlayers)
223269
exports('deleteUserId', API.users.delete)
224270
exports('migrateUserId', API.users.migrate)
271+
exports('addUserIdPrincipal', API.users.addPrincipal)
272+
exports('removeUserIdPrincipal', API.users.removePrincipal)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Storage.dataToPrincipal = {}
2+
3+
local rootKey = 'dataToPrincipal:'
4+
5+
---@param dataId integer
6+
---@param principal string?
7+
---@return string key
8+
local function key(dataId, principal)
9+
return rootKey .. tostring(dataId) .. ':' .. (principal or '')
10+
end
11+
12+
---@param dataId integer
13+
---@param principal string
14+
---@return boolean exists
15+
function Storage.dataToPrincipal.exists(dataId, principal)
16+
return GetResourceKvpString(key(dataId, principal)) ~= nil
17+
end
18+
19+
---@param dataId integer
20+
---@return string[] principals
21+
function Storage.dataToPrincipal.getAll(dataId)
22+
local principals = {}
23+
24+
Utils.searchKvp(key(dataId), function(idKey)
25+
principals[#principals + 1] = GetResourceKvpString(idKey)
26+
end)
27+
28+
return principals
29+
end
30+
31+
---@param dataId integer
32+
---@param principal string
33+
function Storage.dataToPrincipal.set(dataId, principal)
34+
SetResourceKvpNoSync(key(dataId, principal), principal)
35+
end
36+
37+
---@param dataId integer
38+
---@param principal string
39+
function Storage.dataToPrincipal.delete(dataId, principal)
40+
DeleteResourceKvpNoSync(key(dataId, principal))
41+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Storage.userToPrincipal = {}
2+
3+
local rootKey = 'userToPrincipal:'
4+
5+
---@param userId integer
6+
---@param principal string?
7+
---@return string key
8+
local function key(userId, principal)
9+
return rootKey .. tostring(userId) .. ':' .. (principal or '')
10+
end
11+
12+
---@param userId integer
13+
---@param principal string
14+
---@return boolean exists
15+
function Storage.userToPrincipal.exists(userId, principal)
16+
return GetResourceKvpString(key(userId, principal)) ~= nil
17+
end
18+
19+
---@param userId integer
20+
---@return string[] principals
21+
function Storage.userToPrincipal.getAll(userId)
22+
local principals = {}
23+
24+
Utils.searchKvp(key(userId), function(idKey)
25+
principals[#principals + 1] = GetResourceKvpString(idKey)
26+
end)
27+
28+
return principals
29+
end
30+
31+
---@param userId integer
32+
---@param principal string
33+
function Storage.userToPrincipal.set(userId, principal)
34+
SetResourceKvpNoSync(key(userId, principal), principal)
35+
end
36+
37+
---@param userId integer
38+
---@param principal string
39+
function Storage.userToPrincipal.delete(userId, principal)
40+
DeleteResourceKvpNoSync(key(userId, principal))
41+
end

0 commit comments

Comments
 (0)