我在一个应用程序中使用Parse SDK,用于拥有大约3000名客户的小型企业。我使用内置类PFUser在首次启动时注册所有用户,但我还需要:
为此,我正在考虑这个过程:
代码如下所示:
- (void)signUpNewUser {
NSString *userMail = self.mailTextField.text;
NSString *userPassword = self.passwordTextField.text;
NSString *userKeyInput = self.userKeyTextField.text;
// == 1. Check Mail ==
if (![self validateEmail:userMail]) {
[self showAlertWithTitle:@"Mail invalide" subTitle:@"Entrez une adresse mail valide."];
return;
}
// == 2. Check Password ==
if ([self validatePassword:userPassword]) {
// == 3. Check UserKey Input Format ==
if ([self validateUserKeyFormat:userKeyInput]) {
// == 4. Check if this UserKey exists ==
PFQuery *userKeyQuery = [PFQuery queryWithClassName:@"UserKey"];
[userKeyQuery whereKey:@"userKeyString" equalTo:userKeyInput];
PFObject *fetchedUserKey = [userKeyQuery getFirstObject];
// == 4.1. UserKey does not exist ==
if (!fetchedUserKey) {
[self showAlertWithTitle:@"Clé inconnue" subTitle:@"La clé saisie n'existe pas. Demandez au responsable du Club une clé personnelle différente."];
return;
}
// == 4.2. UserKey exists, check if it is available ==
if (fetchedUserKey) {
// == 5.1. UserKey is already taken ==
if (fetchedUserKey[@"user"] != nil) {
[self showAlertWithTitle:@"Clé déjà utilisée" subTitle:@"La clé saisie est déjà utilisée par un autre membre du Club. Demandez au responsable du Club une clé personnelle différente."];
return;
}
// == 5.2. UserKey is free. Create new PFUser, link with userKey & signUp ==
if (fetchedUserKey[@"user"] == nil) {
PFUser *newUser = [PFUser user];
newUser.username = userMail;
newUser.password = userPassword;
newUser[@"isAdmin"] = fetchedUserKey[@"isAdmin"];
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"\n\n ** signUp ErrorDesc: %@\nError Code= %i",error.localizedDescription,error.code);
}
if (!error) {
NSLog(@"\n\n ** signUp SUCCESS!");
// I assume Parse will automatically create a One-to-One relationship between the UserKey and the PFUser. Am I right ?
fetchedUserKey[@"user"] = newUser;
[fetchedUserKey save];
newUser[@"userKey"] = fetchedUserKey;
[newUser save];
[self dismissViewControllerAnimated:YES completion:nil];
}
}];
}
}
}
}
}
我已经测试了代码,它运行正常并处理各种情况(iPhone丢失,输入格式错误,已经使用了userKey等)。
但是因为这是我第一次在身份验证时限制用户访问权限,所以我想知道这种做事方式是否正确。