Setting ACL for new Parse user in swift

923 views Asked by At

Im currently running into a problem with users having access to objects of another user in my app.

I know how to set the ACL when a user creates an object when they are already logged into the app, but I don't know how to set the ACL for someone signing up. When i set a navigation title to display the ["BusinessName"] column of a user, I receive business names from another created users column....thanks for your help!! greatly appreciated!

 @IBAction func signupFinalButton(sender: AnyObject) {

  var newUser = PFUser()
  newUser.username = username
  newUser.password = password
  newUser.email = email
  newUser["FirstName"] = firstName
  newUser["LastName"] = lastName
  newUser["BusinessName"] = businessName
  newUser["City"] = city
  newUser["State"] = state

  // This isn't seeming to work...
  newUser.ACL = PFACL(user: PFUser.currentUser()!) 
  or this 
  newUser.ACL = PFACL(user: PFUser())

  newUser.signUpInBackgroundWithBlock({ (succeed, error) -> Void in

or maybe I am querying parse wrong? Im new to it.

     func navTitle () {
     var user = PFUser.currentUser()
     var query = PFQuery(className:"_User")
     query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) -> Void in

     if error == nil {
     if let objects = objects as? [PFObject] {
     for object in objects {

     self.homeScreen.title = object["BusinessName"] as! String?
      }
     }else {

     self.homeScreen.title = "Home"
     }
2

There are 2 answers

8
DogCoffee On BEST ANSWER

You need to set the ACL after the signUp has completed.

So in the delegate method,

func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser)

// set the ACL for the newly created user

newUser.ACL = PFACL(user: PFUser.currentUser()!)

//Then you can save the user, ie saveEventaully 

If you are not using the PFSignUpViewController - just set the ACL in the code block of signUpInBackgroundWithBlock, then call the save once its been set.

This is the same when you want to create a role, the user needs to already exisit.

This is how to create a role within the same method body

let role = PFRole(name: user.objectId!, acl: roleACL)
role.users.addObject(user)

When first learning parse, i suggest creating new users via this method.

0
Clifton Labrum On

What @DogCoffee says is right, but there's an even simpler way to handle it.

In your app delegate inside your didFinishLaunchingWithOptions function, you can set a default that will always use the appropriate ACL. You don't have to manually set the ACL for signing up new users, logging in existing users, saving objects, etc. It's all automatic.

Here's what my Parse stuff in my app delegate looks like and works great:

ParseCrashReporting.enable()
Parse.enableLocalDatastore()

Parse.setApplicationId("<YOUR APP ID>", clientKey: "<YOUR CLIENT KEY>")

//Always set the ACL value to the current user
PFACL.setDefaultACL(PFACL(), withAccessForCurrentUser: true)  

I hope that helps. Good luck!