⚠️ This LoopBack 3 example project is no longer maintained. Please refer to LoopBack 4 Examples instead. ⚠️
$ git clone https://github.com/strongloop/loopback-example-access-control
$ cd loopback-example-access-control
$ npm install
$ node .
In this example, we create "Startkicker" (a basic Kickstarter-like application) to demonstrate authentication and authorization mechanisms in LoopBack. The application consists of four types of users:
guestownerteam memberadministratorEach user type has permission to perform tasks based on their role and the application’s ACL (access control list) entries.
loopback-example-access-controlloopback-example-access-control$ lb app loopback-example-access-control
... # follow the prompts
$ cd loopback-example-access-control
user
db (memory)UserNoteam
db (memory)PersistedModelNoownerId
memberId
project
db (memory)PersistedModelYesname
balance
No properties are required for the
usermodel because we inherit them from the built-inUsermodel by specifying it as the base class.
$ lb model user
... # follow the prompts, repeat for `team` and `project`
Define three remote methods in project.js:
user
project
projectsownerIdteam
teamsownerIdteam
user
membersmemberIdproject
user
userownerIdCreate a boot script named sample-models.js.
This script does the following:
John, Jane, and
Bob)John as the owner, and adds John and Jane as team
members Jane as the owner and solo team
member admin and adds a role mapping to make Bob an
admin LoopBack comes preconfigured with EJS out-of-box. This means we can use server-side templating by simply setting the proper view engine and a directory to store the views.
Create a views directory to store server-side templates.
$ mkdir server/views
Create index.ejs in the views directory.
Configure server.js to use server-side
templating. Remember to import the path package.
Create routes.js. This script does the following:
GET / route to render index.ejs GET /projects route to render projects.ejs POST /projects route to to render projects.ejs when credentials are valid and renders index.ejs when credentials are invalidGET /logout route to log the user out When you log in sucessfully,
projects.htmlis rendered with the authenticated user’s access token embedded into each link.
Create the views directory to store views.
In this directory, create index.ejs and projects.ejs.
Create role-resolver.js.
This file checks if the context relates to the project model and if the request maps to a user. If these two requirements are not met, the request is denied. Otherwise, we check to see if the user is a team member and process the request accordingly.
ACLs are used to restrict access to application REST endpoints.
(all existing models)All methods and propertiesAll (match all types)All usersExplicitly deny accessGET /api/projects/listProjects
projectA single methodlistProjectsAll usersExplicitly grant accessGET /api/projects
projectA single methodfindotheradminExplicitly grant accessGET /api/projects/:id
projectA single methodfindByIdotherteamMemberExplicitly grant accessPOST /api/projects/donate
projectA single methoddonateAny authenticated userExplicitly grant accessPOST /api/projects/withdraw
projectA single methodwithdrawThe user owning the objectExplicitly grant access$ lb acl
# follow the prompts, repeat for each ACL listed above
Start the server (node .) and open localhost:3000 in your browser to view the app. You will see logins and explanations related to each user type we created:
Guest
Project owner
Project team member
Administrator