/    Sign up×
Community /Pin to ProfileBookmark

Pass In Permissions

I’ve got a range of classes to manage a website. They’re all included via this script at the top of each page

“`
require_once ‘OOP/Dbh.php’;
foreach (glob(“OOP/*.php”) as $filename){
require_once $filename;
}
“`

One of these is a Session Class (might change this to cookie) but it will work the same way. It gets, sets and returns sessions names/values. Such as logged in status, logged in user etc. Another class is a User Class which manages users.

One method is to look up a database and see if any given function is available to that user. That method is called $User->checkAccess($) where the $ relates to the corresponding function (1 if I was editing a user as per below).

I do this by:

Having a table “availableFunctions”” which will have a row functionId: 1, functionName: ‘Edit Users’. There’s also a “userFunctions” table which has functionId: 1, userId: 1. Going back up to the example above, in that function I do a SQL query, if the a row exists with my logged in UserID and the function I’m attempting to do, I get true and display the page otherwise false.

This works!

However, is there a way inside each function across my collection of classes where I can reference back and check them. This way I don’t forget any other forms/processes which use the same function.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 15.2021 — If I'm understanding correctly, you basically want a sort of global place to store user config stuff during a script execution (i.e. versus something like using $_SESSION across multiple requests)?

If so, this might be a use case for a "singleton" class, which could be accessed from any other class. (I'll leave it to you to search on "singleton class". ;) ). However, singletons can add a layer of dependency that is somewhat contrary to good OOP practices, so I'd prefer to just create something like a Config class or whatever makes sense, instantiate it in your start-up script, and then explicitly pass it to the constructor of any other class that needs to use it (whether read and/or write access). I'll often do the same thing with a database class/object, passing it into the constructor of each class that needs to use it.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...