Skip to content

Commit 7cd04a5

Browse files
committed
Merge pull request GmodStarfall#122 from Xandaros/permissions
Permissions!
2 parents 75d2a21 + 8e890ae commit 7cd04a5

21 files changed

Lines changed: 586 additions & 130 deletions

File tree

lua/entities/starfall_screen/cl_init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ENT.RenderGroup = RENDERGROUP_OPAQUE
55
include( "starfall/SFLib.lua" )
66
assert( SF, "Starfall didn't load correctly!" )
77

8-
local context = SF.CreateContext( nil, nil, nil, nil, SF.Libraries.CreateLocalTbl{ "render" } )
8+
local context = SF.CreateContext( nil, nil, nil, SF.Libraries.CreateLocalTbl{"render"} )
99

1010
surface.CreateFont( "Starfall_ErrorFont", {
1111
font = "arial",

lua/starfall/compiler.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function SF.Compiler.Compile(code, context, mainfile, player, data, dontpreproce
3636
instance.initialized = false
3737
instance.context = context
3838
instance.mainfile = mainfile
39-
instance.permissions = setmetatable({},context.permissions)
4039

4140
for filename, source in pairs(code) do
4241
if not dontpreprocess then
@@ -56,6 +55,5 @@ function SF.Compiler.Compile(code, context, mainfile, player, data, dontpreproce
5655
end
5756
end
5857

59-
instance.permissions:assign(instance)
6058
return true, instance
6159
end

lua/starfall/database.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
SF.DB = {}
2+
3+
SF.DB.query = sql.Query
4+
SF.DB.querySingleValue = sql.QueryValue
5+
6+
function SF.DB.querySingleRow ( query )
7+
return sql.QueryRow( query, 0 )
8+
end
9+
10+
function SF.DB.escape ( str )
11+
return sql.SQLStr( str ):sub( 2, -2 )
12+
end
13+
14+
local function queryMultiple ( statements )
15+
local ret = nil
16+
for k,v in pairs( statements ) do
17+
ret = sql.Query( v )
18+
if ret == false then break end
19+
end
20+
return ret
21+
end
22+
23+
-- check whether the tables exist and, if not, import the schema
24+
if sql.TableExists( "starfall_meta" ) then
25+
local version = sql.QueryValue(
26+
"SELECT value FROM starfall_meta WHERE key='schema_version'" )
27+
if not version then
28+
error( "starfall tables exists but couldn't get schema version" )
29+
elseif "0.1" ~= version then
30+
error( "starfall DB schema exists but is wrong version" )
31+
end
32+
else
33+
sql.Begin()
34+
local result = queryMultiple( {
35+
[==[ -- bits of meta-information about Starfall
36+
CREATE TABLE starfall_meta (
37+
key TEXT NOT NULL PRIMARY KEY,
38+
value TEXT NOT NULL
39+
)]==],
40+
41+
[==[ INSERT INTO starfall_meta VALUES ("schema_version", "0.1")]==],
42+
43+
[==[ -- grants permissions to roles
44+
CREATE TABLE starfall_perms_grants (
45+
role INTEGER NOT NULL, -- 0 = user, 1 = admin, 2 = superadmin
46+
key TEXT NOT NULL,
47+
grant INTEGER CHECK (grant IN (0, 1, 2)), -- 0 = NEUTRAL, 1 = ALLOW, 2 = DENY
48+
PRIMARY KEY (role, key)
49+
)]==]
50+
} )
51+
sql.Commit()
52+
53+
if result == false then
54+
error( "error importing Starfall schema " .. sql.LastError() )
55+
end
56+
end

lua/starfall/instance.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ SF.Instance.__index = SF.Instance
1717
-- @field hooks Registered hooks
1818
-- @field scripts The compiled script functions.
1919
-- @field initialized True if initialized, nil if not.
20-
-- @field permissions Permissions manager
2120
-- @field error True if instance is errored and should not be executed
2221
-- @field mainfile The main file
2322
-- @field player The "owner" of the instance

lua/starfall/libs_cl/entities.lua

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ local ents_lib = SF.Entities.Library
88
local ents_metatable = SF.Entities.Metatable
99
local wrap, unwrap = SF.Entities.Wrap, SF.Entities.Unwrap
1010

11-
SF.Permissions:registerPermission({
12-
name = "Modify Entities",
13-
desc = "Allow modification of entities clientside",
14-
level = 1,
15-
value = false,
16-
})
17-
1811
local isValid = SF.Entities.IsValid
1912
local getPhysObject = SF.Entities.GetPhysObject
2013

lua/starfall/libs_sh/entities.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ local wrap, unwrap = SF.CreateWrapper(ents_metamethods,true,true,debug.getregist
1313
-- @shared
1414
local ents_lib, _ = SF.Libraries.Register("entities")
1515

16+
-- Register privileges
17+
do
18+
local P = SF.Permissions
19+
P.registerPrivilege( "entities.setColor", "Set Color", "Allows the user to change the color of an entity" )
20+
end
21+
1622
-- ------------------------- Internal functions ------------------------- --
1723

1824
SF.Entities.Wrap = wrap
@@ -94,6 +100,7 @@ function ents_methods:setColor( clr )
94100

95101
local this = unwrap( self )
96102
if IsValid( this ) then
103+
if not SF.Permissions.check( SF.instance.player, this, "entities.setColor" ) then return end
97104
this:SetColor( clr )
98105
this:SetRenderMode( clr.a == 255 and RENDERMODE_NORMAL or RENDERMODE_TRANSALPHA )
99106
end

lua/starfall/libs_sh/files.lua

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--- TODO: Permissions System before fixing this.
1+
--- TODO: fix this.
22
--- TODO: Add VON encoding of any table's that are passed, work on 'universal' serializer and deserializer
33
-------------------------------------------------------------------------------
44
-- File functions
@@ -8,20 +8,13 @@
88
-- @shared
99
local files_library, _ = SF.Libraries.Register("files")
1010

11-
--- Access Files permission
12-
-- @name Access Files Permission
13-
-- @class table
14-
-- @field name "Access Files"
15-
-- @field desc "Allows access to data/starfallscriptdata/"
16-
-- @field level 1
17-
-- @field value True if clientside, false if serverside
18-
19-
SF.Permissions:registerPermission({
20-
name = "Access Files",
21-
desc = "Allows access to data/starfallscriptdata/",
22-
level = 1,
23-
value = 1,
24-
})
11+
-- Register privileges
12+
do
13+
local P = SF.Permissions
14+
P.registerPrivilege( "file.read", "Read files", "Allows the user to read files from data/starfallscript directory" )
15+
P.registerPrivilege( "file.write", "Write files", "Allows the user to write files to data/starfallscript directory" )
16+
P.registerPrivilege( "file.exists", "efile xistence check", "Allows the user to determine whether a file data/starfallscript exists" )
17+
end
2518

2619
file.CreateDir("starfallscriptdata/")
2720

@@ -30,9 +23,9 @@ file.CreateDir("starfallscriptdata/")
3023
-- @return Contents, or nil if error
3124
-- @return Error message if applicable
3225
function files_library.read(path)
26+
if not SF.Permissions.check( SF.instance.player, path, "file.read" ) then SF.throw( "Insufficient permissions", 2 ) end
3327
SF.CheckType(path, "string")
3428
if path:find("..",1,true) then error("path contains '..'") return end
35-
if not SF.instance.permissions:checkPermission("Access Files") then error("access denied") return end
3629
local contents = file.Read("starfallscriptdata/"..path, "DATA")
3730
if contents then return contents else error("file not found") return end
3831
end
@@ -42,10 +35,10 @@ end
4235
-- @return True if OK, nil if error
4336
-- @return Error message if applicable
4437
function files_library.write(path, data)
38+
if not SF.Permissions.check( SF.instance.player, path, "file.write" ) then SF.throw( "Insufficient permissions", 2 ) end
4539
SF.CheckType(path, "string")
4640
SF.CheckType(data, "string")
4741
if path:find("..",1,true) then error("path contains '..'") return end
48-
if not SF.instance.permissions:checkPermission("Access Files") then error("access denied") return end
4942
file.Write("starfallscriptdata/"..path, data)
5043
return true
5144
end
@@ -55,10 +48,10 @@ end
5548
-- @param data String that will be appended to the file.
5649
-- @return Error message if applicable
5750
function files_library.append(path,data)
51+
if not SF.Permissions.check( SF.instance.player, path, "file.write" ) then SF.throw( "Insufficient permissions", 2 ) end
5852
SF.CheckType(path, "string")
5953
SF.CheckType(data, "string")
6054
if path:find("..",1,true) then error("path contains '..'") return end
61-
if not SF.instance.permissions:checkPermission("Access Files") then error("access denied") return end
6255
file.Append("starfallscriptdata/"..path, data)
6356
return true
6457
end
@@ -68,9 +61,9 @@ end
6861
-- @return True if exists, false if not, nil if error
6962
-- @return Error message if applicable
7063
function files_library.exists(path)
64+
if not SF.Permissions.check( SF.instance.player, path, "file.exists" ) then SF.throw( "Insufficient permissions", 2 ) end
7165
SF.CheckType(path, "string")
7266
if path:find("..",1,true) then error("path contains '..'") return end
73-
if not SF.instance.permissions:checkPermission("Access Files") then error("access denied") return end
7467
return file.Exists("starfallscriptdata/"..path, "DATA")
7568
end
7669

@@ -79,9 +72,9 @@ end
7972
-- @return True if successful, nil if error
8073
-- @return Error message if applicable
8174
function files_library.delete(path)
75+
if not SF.Permissions.check( SF.instance.player, path, "file.write" ) then SF.throw( "Insufficient permissions", 2 ) end
8276
SF.CheckType(path, "string")
8377
if path:find("..",1,true) then error("path contains '..'") return end
84-
if not SF.instance.permissions:checkPermission("Access Files") then error("access denied") return end
8578
if not file.Exists("starfallscriptdata/"..path, "DATA") then error("doesn't exist") return end
8679
file.Delete(path)
8780
return true

lua/starfall/libs_sh/find.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
-- @shared
77
local find_library, _ = SF.Libraries.Register("find")
88

9+
-- Register privileges
10+
do
11+
local P = SF.Permissions
12+
P.registerPrivilege( "find", "Find", "Allows the user to access the find library" )
13+
end
14+
915
local find_cooldown
1016
if SERVER then
1117
find_cooldown = CreateConVar("sf_find_cooldown_sv", "0.01", {FCVAR_REPLICATED, FCVAR_ARCHIVE, FCVAR_DONTRECORD})
@@ -41,6 +47,7 @@ end
4147
--- Checks if a find function can be performed
4248
-- @return True if find functions can be used
4349
function find_library.canFind()
50+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then return false end
4451
local data = SF.instance.data
4552
if not data.findcooldown then data.findcooldown = 0 end
4653
return data.findcooldown <= CurTime()
@@ -52,6 +59,7 @@ end
5259
-- @param filter Optional function to filter results
5360
-- @return An array of found entities
5461
function find_library.inBox(min, max, filter)
62+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
5563
SF.CheckType(min,"Vector")
5664
SF.CheckType(max,"Vector")
5765
if filter then SF.CheckType(filter,"function") end
@@ -68,6 +76,7 @@ end
6876
-- @param filter Optional function to filter results
6977
-- @return An array of found entities
7078
function find_library.inSphere(center, radius, filter)
79+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
7180
SF.CheckType(center,"Vector")
7281
SF.CheckType(radius,"number")
7382

@@ -85,6 +94,7 @@ end
8594
-- @param filter Optional function to filter results
8695
-- @return An array of found entities
8796
function find_library.inCone(pos, dir, distance, radius, filter)
97+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
8898
SF.CheckType(pos,"Vector")
8999
SF.CheckType(dir,"Vector")
90100
SF.CheckType(distance,"number")
@@ -101,6 +111,7 @@ end
101111
-- @param filter Optional function to filter results
102112
-- @return An array of found entities
103113
function find_library.byClass(class, filter)
114+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
104115
SF.CheckType(class,"string")
105116

106117
local instance = SF.instance
@@ -114,6 +125,7 @@ end
114125
-- @param filter Optional function to filter results
115126
-- @return An array of found entities
116127
function find_library.byModel(model, filter)
128+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
117129
SF.CheckType(model,"string")
118130

119131
local instance = SF.instance
@@ -126,6 +138,7 @@ end
126138
-- @param filter Optional function to filter results
127139
-- @return An array of found entities
128140
function find_library.allPlayers(filter)
141+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
129142
local instance = SF.instance
130143
if not updateCooldown( instance ) then SF.throw( "You cannot run a find right now; use 'find_library.canFind()'", 2 ) end
131144

@@ -136,6 +149,7 @@ end
136149
-- @param filter Optional function to filter results
137150
-- @return An array of found entities
138151
function find_library.all(filter)
152+
if not SF.Permissions.check( SF.instance.player, nil, "find" ) then SF.throw( "Insufficient permissions", 2 ) end
139153
local instance = SF.instance
140154
if not updateCooldown(instance) then SF.throw( "You cannot run a find right now; use 'find_library.canFind()'", 2 ) end
141155

lua/starfall/libs_sh/sound.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
--- Sound functions. Plays and manipulates sounds, optionally
32
-- attaching them to an entity.
43

@@ -8,13 +7,21 @@ local sound_library, _ = SF.Libraries.Register("sounds")
87
local sound_methods, sound_metamethods = SF.Typedef("Sound")
98
local wrap, unwrap = SF.CreateWrapper(sound_metamethods,true,false)
109

10+
-- Register privileges
11+
do
12+
local P = SF.Permissions
13+
P.registerPrivilege( "sound.create", "Sound", "Allows the user to create sounds" )
14+
P.registerPrivilege( "sound.modify", "Sound", "Allows the user to modify sounds" )
15+
end
16+
1117
--- Creates a sound and attaches it to an entity. You need to do sound:Play() before
1218
-- the sound will play however.
1319
-- @param path Filepath to the sound file
1420
-- @param entity Entity playing the sound
1521
-- @return The sound object. Keep this around to ensure it isn't GC'd (and thus stopped)
1622
-- before it is done playing.
1723
function sound_library.create(entity, path)
24+
if not SF.Permissions.check( SF.instance.player, path, "sound.create" ) then SF.throw( "Insufficient permissions", 2 ) end
1825
SF.CheckType(path, "string")
1926
SF.CheckType(entity, SF.Entities.Metatable)
2027
if path:match('["?]') then SF.throw( "Invalid sound path: " .. path, 2 ) end
@@ -28,6 +35,7 @@ end
2835
-- @param amplitude (Optinal) Loudness of the sound, from 0 to 255
2936
-- @param pitch (Optional) Pitch percent, from 0 to 255
3037
function sound_library.emitWorld(origin, path, amplitude, pitch)
38+
if not SF.Permissions.check( SF.instance.player, path, "sound.create" ) then SF.throw( "Insufficient permissions", 2 ) end
3139
SF.CheckType(path, "string")
3240
SF.CheckType(origin, "Vector")
3341
if amplitude then
@@ -48,6 +56,7 @@ end
4856
-- @param soundlevel (Optional) The sound level. See sound:setLevel() for more info
4957
-- @param pitch (Optional) Pitch percent, from 0 to 255
5058
function sound_library.emitEntity(entity, path, soundlevel, pitch)
59+
if not SF.Permissions.check( SF.instance.player, path, "sound.create" ) then SF.throw( "Insufficient permissions", 2 ) end
5160
SF.CheckType(entity, SF.Entities.Metatable)
5261
SF.CheckType(path, "string")
5362
if soundlevel then
@@ -77,6 +86,7 @@ end
7786

7887
--- Plays the sound.
7988
function sound_methods:play()
89+
if not SF.Permissions.check( SF.instance.player, this, "sound.modify" ) then SF.throw( "Insufficient permissions", 2 ) end
8090
SF.CheckType(self, sound_metamethods)
8191
unwrap(self):Play()
8292
end
@@ -85,6 +95,7 @@ end
8595
-- @param pitch The sound pitch as a percent from 0 to 255
8696
-- @param delta (Optinal) The transition time between the current pitch and the new one
8797
function sound_methods:setPitch(pitch, delta)
98+
if not SF.Permissions.check( SF.instance.player, this, "sound.modify" ) then SF.throw( "Insufficient permissions", 2 ) end
8899
SF.CheckType(self, sound_metamethods)
89100
SF.CheckType(pitch, "number")
90101
if delta then
@@ -97,6 +108,7 @@ end
97108
--- Sets the sound volume
98109
-- @param vol Volume as a percent between 0 and 1
99110
function sound_methods:setVolume(vol)
111+
if not SF.Permissions.check( SF.instance.player, this, "sound.modify" ) then SF.throw( "Insufficient permissions", 2 ) end
100112
SF.CheckType(self, sound_metamethods)
101113
SF.CheckType(vol, "number")
102114
unwrap(self):ChangeVolume(math.Clamp(vol,0,1))
@@ -107,6 +119,7 @@ end
107119
-- (use decibel value from the 'code' column, not the actual enum or 'value' column).
108120
-- @param level New level
109121
function sound_methods:setLevel(level)
122+
if not SF.Permissions.check( SF.instance.player, this, "sound.modify" ) then SF.throw( "Insufficient permissions", 2 ) end
110123
SF.CheckType(self, sound_metamethods)
111124
SF.CheckType(level, "number")
112125
unwrap(self):SetSoundLevel(math.Clamp(level, 0, 511))
@@ -115,6 +128,7 @@ end
115128
--- Stops or fades out the sound.
116129
-- @param fade (Optional) Time, in seconds, to fade out the sound. Not given = stop immediately
117130
function sound_methods:stop(fade)
131+
if not SF.Permissions.check( SF.instance.player, this, "sound.modify" ) then SF.throw( "Insufficient permissions", 2 ) end
118132
SF.CheckType(self, sound_metamethods)
119133
if fade then
120134
SF.CheckType(fade, "number")

0 commit comments

Comments
 (0)