Which is the best way to set admin to your application?

Hello everyone!

Would you please suggest an example to create admin/admins to your application?
Thank you!

This is something the platform makes very easy, you can just grab the caller of a public method and run a check on it to allow admin-only access, eg:

public shared(msg) func doSomething() {
    if (isAdmin(msg.caller)) {
	...

msg.caller there is of type Principal. You could either store this Principal in an array/set of admin Principals, or you could have an admin status flag in the user’s profile. The isAdmin() function above could check for either of these.

A couple of examples from project repos:


Or
6 Likes

Where in the LinkedUp example does an admin ever get added to the adminIds array? In the docs there is an initializer that seems to be saved for a class, but I do not see how this is used for an actor:

How do you initially save the deploying principal as an admin/owner? In solidity you’d save this in the constructor.

On deployment the deploying identity’s principal gets assigned to the variable “initializer” in the example you posted, you could then later just check if a caller matches this variable within your canister’s methods:

public shared(msg) func doSomething() {
    if (msg.caller == initializer) {
	...

thanks for this. Does this happen with an actor as well as a class, or just a class?

for example:

actor my_canister {

vs

shared ({ caller = initializer }) actor class my_canister() {

do both have access to initializer?

I suppose I was looking for this information:

Simple actor declarations do not let you access their installer. If you need access to the installer of an actor, rewrite the actor declaration as a zero-argument actor class instead.

1 Like