Skip to content

Are foreign keys supposed to populate automatically? #459

Closed

Description

@dagumak

I am using mongodb, and I have a TestUsers has many Messages relatinship. I am have my access token set after logging in. I am making a POST to /messages, but it does not populate the foreign key automatically. Also, when I try to use my users id to access the messages, it gives me a 401. Did I setup my ACL and relationships properly?

{
  "name": "TestUser",
  "base": "User",
  "strict": true,
  "properties": {
    "username": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "messages": {
      "type": "hasMany",
      "model": "Message",
      "foreignKey": "testUserId"
    }  
  },
  "acls": [

  ],
  "methods": []
}

{
  "name": "Message",
  "base": "PersistedModel",
  "properties": {
    "type": {
      "type": "string",
      "required": true
    },
    "text": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "testUser": {
      "type": "belongsTo",
      "model": "TestUser",
      "foreignKey": "testUserId"
    }
  },
  "acls": [
    {
      "principalType": "User",
      "principalId": "$everyone",
      "permission": "DENY",
      "accessType": "*"
    },
    {
      "principalType": "USER",
      "principalId": "$owner",
      "permission": "ALLOW",
      "accessType": "*"
    }
  ],
  "methods": []
}

Activity

  1. ritch commented on Aug 11, 2014

    @ritch
    Member

    setting my access token before POSTing to B

    I'm not exactly sure what you are doing. If you are doing a POST to /api/messages the foreign key will not be populated. If you post to /api/testusers/$someUserId/messages the key should be added for you.

    The Role.OWNER dynamic role only checks for the following properties:

    • userId
    • owner

    If you modify your relation to use that as a foreign key the 401 should go away.

    Hopefully that helps. If this is not helpful, could you post a code example outlining what you are doing?

  2. dagumak commented on Aug 11, 2014

    @dagumak
    Author

    @ritch Thank you! I made the change and added userId as my foreign key, but is owner supposed to be a boolean, string, or id? Do I just put that as a property on my belongsTo object?

  3. ritch commented on Aug 11, 2014

    @ritch
    Member

    If you changed it to userId you should be good. It checks for either someModel.userId === currentUserId or someModel.owner === currentUserId. It is not a boolean.

  4. ritch commented on Aug 11, 2014

    @ritch
    Member
  5. dagumak commented on Aug 12, 2014

    @dagumak
    Author

    I did a quick trace, and I dont understand why is my model being denied. I tried adding access to all in messages, but still don't really understand why.

    { model: 'TestUser',
      property: '__get__messages',
      accessType: 'EXECUTE',
      permission: 'DENY',
      methodNames: [] }
    
  6. dagumak commented on Aug 19, 2014

    @dagumak
    Author

    @ritch I dug through the documentation, but I cant seem to get it right still. I am logging in to TestUser, then setting the access token and filling in the text field for userId, and making a GET request to /TestUsers/{userId}/messages and it still gives me a 401. This seems pretty straight forward, but it's not working for me; I am really lost. What might I be doing incorrectly? This is what I have right now:

    {
      "name": "TestUser",
      "base": "User",
      "strict": true,
      "properties": {
        "username": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "message": {
          "type": "hasMany",
          "model": "Message",
          "foreignKey": "userId"
        }  
      },
      "acls": [
    
      ],
      "methods": []
    }
    
    
    {
      "name": "Message",
      "base": "PersistedModel",
      "properties": {
        "type": {
          "type": "string",
          "required": true
        },
        "text": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "testUser": {
          "type": "belongsTo",
          "model": "TestUser",
          "foreignKey": "userId"
        }
      },
      "acls": [
        {
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW"
        }
      ],
      "methods": []
    }
    
    
  7. raymondfeng commented on Aug 19, 2014

    @raymondfeng
    Member

    Please add the following ACL entry to your TestUser model:

    {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get_messages"
    }
  8. dagumak commented on Aug 23, 2014

    @dagumak
    Author

    @raymondfeng That worked! Thanks! @fabien Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions