Class: Parse::Constraint::WriteableByConstraint

Inherits:
Parse::Constraint show all
Includes:
AclConstraintHelpers
Defined in:
lib/parse/query/constraints.rb

Overview

Note:

This constraint uses aggregation pipeline because Parse Server restricts direct queries on the internal _wperm field.

ACL Write Permission Query Constraint Query objects based on write permissions using MongoDB’s internal _wperm field. Parse Server restricts direct queries on _wperm, so this uses aggregation pipeline.

Examples:

Find objects with NO write permissions (master key only / read-only)

Song.query.where(:acl.writeable_by => [])

Find objects writable by a specific user ID

Song.query.where(:acl.writeable_by => "userId123")
Song.query.where(:acl.writeable_by => current_user)

Find objects writable by a role

Song.query.where(:acl.writeable_by => "role:Admin")

Direct Known Subclasses

WritableByConstraint

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint using aggregation pipeline.

Returns:

  • (Hash)

    the compiled constraint using aggregation pipeline.



3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
# File 'lib/parse/query/constraints.rb', line 3115

def build
  keys = normalize_acl_keys(@value)

  if keys.empty?
    # Empty array = no write permissions (master key only)
    pipeline = [
      {
        "$match" => {
          "$or" => [
            { "_wperm" => { "$exists" => true, "$eq" => [] } },
            { "_wperm" => { "$exists" => false } },
          ],
        },
      },
    ]
  else
    # Find objects writable by ANY of the specified keys
    pipeline = [
      {
        "$match" => {
          "_wperm" => { "$in" => keys },
        },
      },
    ]
  end

  { "__aggregation_pipeline" => pipeline }
end

#writeable_byWriteableByConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :acl.writeable_by => []
q.where :acl.writeable_by => "userId"

Returns:



3112
# File 'lib/parse/query/constraints.rb', line 3112

register :writeable_by