There are also times where you need to validate custom conditions and block access to certain areas. This way, you can implement your own custom security logic and leverage cbSecurity for blockage. You will accomplish this via the secureWhen()
method:
The context
can be a closure/lambda/udf or a boolean evaluation:
The closure/udf will receive the currently authenticated user as the first argument.
secure()
MethodsNow that you have access to the model, you can use the following method to verify explicit permissions and authorize access. This method will throw an exception if the user does not validate the incoming permissions context (NotAuthorized
).
The permission
can be an array, string or list of the permissions to validate. The user must have at least one of the permissions specified.
The message
is a custom error message to be used in the message
string of the exception thrown.
You also have two more authorization methods that will verify certain permission conditions for you:
when()
There are also cases where you want to execute a piece of code by determining if the user has access to do so. For example, only a USER_ADMIN
can change people's roles or you want to filter some data for certain users. For this, we have created the when()
method with the following signature:
The permissions
is a permission array or list that will be Or'ed
The success
is a closure/lambda or UDF that will execute if the permissions validate.
The fail
is a closure/lambda or UDF that will execute if the permissions DID not validate, much like an else statement
Both closures/functions takes in a user
which is the currently authenticated user, the called in permissions
and can return anything.
You can also chain the when()
calls if needed, to create beautiful security contexts. So if we go back to our admin examples, we can do something like this:
We have also added the following whenX()
methods to serve your needs when evaluating the permissions:
This object is used to provide you with human, fluent and explicit security authorizations and contexts.
The cbSecurity
model is a specialized service that will allow you to do explicit authorizations in any layer of your ColdBox application.
There will be times where you will need authorization checks outside of the incoming request rules or the handler annotations. This can be from within interceptors, models, layouts or even views. For this, we have provided the cbSecurity
model so you can do explicit authorization checks anywhere you like.
cbSecurity
ModelYou can inject our model or you can use our handy cbsecure()
mixin (handlers/layouts/views) and then call the appropriate security functions:
All security methods will call the application's configured Authentication Service to retrieve the currently logged in user. If the user is not logged in an immediate NoUserLoggedIn
exception will be thrown by all methods.
You can now discover our sections for securing using cbSecurity
cbSecurity
Method SummaryWhen certain permission context is met, if not throws NotAuthorized
secure( permissions, [message] )
secureAll( permissions, [message] )
secureNone( permissions, [message] )
secureWhen( context, [message] )
guard() alias to secure()
When certain permission context is met, execute the success function/closure, else if a fail
closure is defined, execute that instead.
when( permissions, success, fail )
whenAll( permissions, success, fail )
whenNone( permissions, success, fail )
Verify permissions or user equality
has( permissions ):boolean
all( permissions ):boolean
none( permissions ):boolean
sameUser( user ):boolean
secureView( permissions, successView, failView )
You can also use our handy event.secureView()
method in the request context to pivot between views according to user permissions.
cbSecurity injects the secureView()
method into the request context via the preProcess
interception point.
This will allow you to set the successView
if the user has the permissions or the failView
if they don't.
If you just want to validate if a user has certain permissions or maybe no permissions at all or if a passed user is the same as the logged in user, then you can use the following boolean methods that only do verification.
Please note that you could potentially do these type of methods by leveraging the currently logged in user and it's hasPermission()
method. However, these methods provide abstraction and can easily be mocked!
These are great to have a unified and abstracted way to verifying permissions or if the passed user is the same as the logged in user. Here are some examples:
View Layer
Other Layers:
Please note that we do user equality by calling the getId()
method of the authenticated user and the incoming user. This is part of our IAuthUser
interface requirements.