@luke2’s suggestion is essential. For any “controlled access” type of page – whether that’s an administrator-only page or a page where a registered user makes/changes data, etc. – you usually want to keep all page elements hidden by default and only show them via the page workflow(s) that check that the current user is authorized to view them.
Additionally:
There are many different ways of handling administrator access and a couple have been mentioned above, including:
-
If you’re the only admin, check for Current User’s email is your email address. (This is what I’d call an implied administrator approach.) Similarly, you could check if Current User’s unique ID is your unique ID. [EDIT: These can work for page access, but will not work in data privacy rules as user @luke2 discovers in a later reply.]
-
Have a boolean (yes/no) field on the User object called “Administrator” and set it to yes for Users who should be allowed admin access. (This is an explicit administrator approach. But I’m not sure this is particularly secure, actually, and it would be very easy to accidentally give admin access to some random user.)
Another way to do this – which I personally use and like a lot – is to create what you might call an Administrator “role”. Even if your app doesn’t have a concept of roles, you can do this very easily.
What I do is this (and it’s super secure):
- Create a new data type in your database called Administrator. It needs NO fields at all:
What we do with this object is check if the results of Do a Search for... Administrators contains Current User. If this is true, the User is an Administrator. Read on…
The only way to become an Administrator is for a User (YOU, in this case) to make an Administrator object. Of course this is only possible if you make a workflow in your app that allows this. So, on an admin type page in your app (Dev Mode), drop a button. Give it a very simple workflow:
When Button is clicked... Create a new thing... what thing? Administrator.
Preview your page (and log yourself in of course if you are not logged in) and click the button. Congratulations, you just made yourself an Administrator (in your Dev database)!
You could confirm this by examining your Data > App Data tab like so:
In a moment, we are going to delete that all-powerful button. But first: If you’ve already pushed your app to Live, you have another copy of the database there and your User object may or may not have the same unique ID in both databases. So you either need to (1) push Dev to Live and click that button in your Live mode app. Or alternatively (if your User object is the same in both databases) you can (2) later copy just the Administrator data type from Dev to Live using the database copy features (use caution of course that you only push that new data type – don’t accidentally overwrite your whole database, duh).
If you’ve never pushed your dev app to live, you don’t need to do anything else as, presumably, someday you will push your dev app to live and also copy the database at that point. (And so your User object and this Administrator object will exist in both dev and live versions of your app and be properly associated with each other.)
Now:
Immediately go back to Edit mode and DELETE THAT BUTTON! Now, there is only 1 Administrator in your database (YOU) and there is no way for anyone else to become an Administrator. In fact, even you can’t accidentally create another administrator.
If you had to click the button in Live mode, don’t forget to immediately push dev to live so that the button is also gone from the Live version of your app.
Now we can make this object even more secure. Go to Data > Privacy tab and select the Administrator type. Set up a privacy rule like this (when This Administrator’s Creator is Current User, enable all access. For everyone else, there are NO permissions at all):
This gives the Administrator objects a unique property: They can only be seen by the User that created them. In fact, even another Administrator cannot see who the other Administrators are. The only way to see any info about Administrators is in the back end of your app, in the Data tab. Groovy, right?
Now here’s how we use it. Your page load workflow that checks for Administrator access is like this:
This Search will only return a value when (1) the Current User is logged in and (2) when the Current User is the creator of an Administrator object. It returns nothing - zero, nada, zilch - for other users and will always evaluate to false. And this condition cannot be hacked or manipulated in the browser. It is ultra secure.
But if the Current User is logged in and is an Administrator, this condition evaluates to true (and also reveals no info about any Administrator but themselves).
So in the example above, we show “Group B” which is just a group (set to NOT visible on page load) that contains everything of interest on this administrative page.
For the fail case (User is NOT an Administrator), we just do something like this (redirect somewhere):
Now, as mentioned previously, there’s no way in your app or in your database to successfully create another Administrator except by executing a Create New Thing… Administrator action as the User you want to give admin rights to.
So, if you need to add other administrators, you recreate the magic button and click it on that User’s behalf (using “run as…” User). The high-security way to do this is just create a page that is only viewable by that User (for example “When Current User’s unique ID is [paste that user’s unique id]” show the page, otherwise redirect), put the button there, run the app as that user, click the button, then go delete the page (and if you’re doing this for Live app, don’t forget to push an update so that the page is gone from Live as well).
This sounds like a bunch of rigamarole, but you’re probably only ever going to do this once (for yourself) or maybe a couple of times (for close collaborators).
And then you just don’t ever need to worry about it again and it’s super easy to secure any admin pages, work-in-progress pages, etc. You just drop either or both of the “Do a Search for Administrators’s Creator contains/doesn’t contain Current User” conditions on any page you want to protect, as appropriate.
[EDIT: For more about how you can use this Administrator role as part of other privacy rules – and whether or not this is a good idea – see my further reply down the thread: How do I password protect an entire page? - #7 by keith]