Coder Social home page Coder Social logo

Comments (6)

BuchyOne avatar BuchyOne commented on July 22, 2024 1

Not sure of your POST request, but this from the docs may help:

If a model has a DENY ALL permission (for example a built-in model such as the User model), but related models have no ACLs,the related models will still not be accessible through the User model. So, for example, even if the books model’s default ACL is ALLOW $authenticated for GET /books, the route GET /user/{id}/books default will still be DENY AL

https://loopback.io/doc/en/lb3/Accessing-related-models.html

from loopback-example-access-control.

Nygma297 avatar Nygma297 commented on July 22, 2024

Here are some files

Advertisement.json

{
  "name": "Advertisement",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "adType": {
      "type": "string",
      "required": true
    },
    "name": {
      "type": "string",
      "required": true
    },
    "expiry": {
      "type": "number",
      "required": false
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "Admin",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "SuperAdmin",
      "permission": "ALLOW"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "Admin",
      "permission": "ALLOW",
      "property": ["create", "update", "insert", "deleteById"]
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "SuperAdmin",
      "permission": "ALLOW",
      "property": ["create", "update", "insert", "deleteById"]
    }
  ],
  "methods": {}
}

AppUser.json

{
  "name": "AppUser",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "imageURL": {
      "type": "String"
    },
    "isApproved": {
      "type": "Boolean",
      "required": true,
      "default": false
    },
    "gender": {
      "type": "String",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "bloodRequests": {
      "type": "hasMany",
      "model": "BloodRequest",
      "foreignKey": "userId"
    }
  },
  "acls": [
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "Admin",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "SuperAdmin",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY",
      "property": "create"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "Admin",
      "permission": "ALLOW",
      "property": ["create", "update", "insert", "deleteById"]
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "SuperAdmin",
      "permission": "ALLOW",
      "property": ["create", "update", "insert", "deleteById"]
    }
  ],
  "methods": {}
}

Script.js

s.AppUser;
	var Role = app.models.Role;
	var RoleMapping = app.models.RoleMapping;

	AppUser.create([
		{ isApproved: false, gender: 'Male', username: 'John', email: '[email protected]', password: 'admin' },
		{ isApproved: false, gender: 'Male', username: 'Jane', email: '[email protected]', password: 'superadmin' }
	], function (err, AppUsers) {
		if (err) throw err;

		Role.create({
			name: 'Admin'
		}, function (err, role) {
			if (err) throw err;

			// Make 1st entry as an admin
			role.principals.create({
				principalType: RoleMapping.USER,
				principalId: AppUsers[0].id
			}, function (err, principal) {
				if (err) throw err;
			});
		});
		Role.create({
			name: 'SuperAdmin'
		}, function (err, role) {
			if (err) throw err;

			// Make 2nd entry as an admin
			role.principals.create({
				principalType: RoleMapping.USER,
				principalId: AppUsers[1].id
			}, function (err, principal) {
				if (err) throw err;
			});
		});
	});

from loopback-example-access-control.

Nygma297 avatar Nygma297 commented on July 22, 2024

@BuchyOne

Not sure What you're trying to say, could you please explain

from loopback-example-access-control.

Nygma297 avatar Nygma297 commented on July 22, 2024

@BuchyOne

Done! But the problem still persists. I found out that there was no relation between the roleMapping model and My AppUser model(base: User) due to which the principalId field in roleMapping model doesn't return an object just a string containing the ID of User. I've fixed it temporarily by assigning relations.

I hope it works πŸ‘

Gonna keep this thread open if I get any further queries.

from loopback-example-access-control.

kristojorg avatar kristojorg commented on July 22, 2024

@Nygma297 could you please explain what you did here? I'm having this same issue and can't seem to resolve it. I created an admin role, added a AppUser extended from User to it, and created an acl similar to yours to allow admins to access full api. The admin user is still being denied access however.

I also have a relation from AppUser to Role through RoleMapping:

  "relations": {
    "roles": {
      "type": "hasMany",
      "model": "Role",
      "foreignKey": "principalId",
      "through": "RoleMapping"
    }
  }

Do I need to register a resolver for this static role? Any help would be appreciated.

from loopback-example-access-control.

stale avatar stale commented on July 22, 2024

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

from loopback-example-access-control.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.