简体   繁体   中英

parse swift add new user to existing role

Can some one just confirm that in order to add a user to a existing role the role needs to have public read & write access ?

as this seems to be the only way i can get it to work?

Code to create the Role (Working Fine)

                   let roleACL = PFACL()
                   roleACL.setPublicReadAccess(true)

                   //roleACL.setPublicWriteAccess(true)

                   let role = PFRole(name: "ClubName", acl:roleACL)

                   role.saveInBackground()

Code to add user to said Role (Works If write access set to public)

                    let QueryRole = PFRole.query()

                    QueryRole!.whereKey("name", equalTo: "ClubName")

                    QueryRole!.getFirstObjectInBackgroundWithBlock({ (roleObject: PFObject?, error: NSError?) -> Void in

                        if error == nil

                        {

                            let roleToAddUser = roleObject as! PFRole

                            roleToAddUser.users.addObject(user)

                            roleToAddUser.saveInBackground()

                            //print(roleObject)

                        }

                        else

                        {

                            print(error)

                            //print(roleObject)

                        }


                    })

the above code works but as i said only when the public write access to the role has been set true.

this is driving me crazy now

also IF the role is meant to have the public write access doesn't that make it vulnerable to someone changing the role?

if the role shouldn't have public write access then can someone point me in the right direction to get the above code working without setting the role with public write access.

the error i get if there is no public write access on the role is: object not found for update (Code: 101, Version: 1.8.1)

Why not perform all the work in cloud code instead, that will get you around the issue of read/write issue. Here is what I am doing.

Important to note: The code below would not work when the cloud code JDK was set to 'latest', I had to set it to JDK 1.5 and then redepled my cloud code.

Parse.Cloud.afterSave(Parse.User, function(request) {

            Parse.Cloud.useMasterKey();
            var user = request.object;
            if (user.existed()) {
                //console.log('object exits');
                response.success();
                // console.log('********return after save');
                return;
            }    

            // set ACL so that it is not public anymore
            var acl = new Parse.ACL(user);
            acl.setPublicReadAccess(false);
            acl.setPublicWriteAccess(false);
            user.setACL(acl);
            user.save();
            //add user to role         
            var query = new Parse.Query(Parse.Role);
            query.equalTo("name", "signedmember");
            query.first().then(function(object) {
            if (object) {
                object.relation("users").add(request.user);
                object.save(null, {
                    success: function(saveObject) {
                    object.relation("users").add(request.user);
                    object.save(null, {
                        success: function(saveObject) {
                            // The object was saved successfully.
                            console.log('assigned user to role');
                        },
                        error: function(saveObject, error) {
                            // The save failed.
                            console.error("Failed creating role with error: " + error.code + ":"+ error.message);
                        }
                    });
                    },
                });
            }
        });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM