我正在尝试建立一个所有用户都可以访问的共享领域。我还打算让用户根据需要创建领域,代表项目。我想授予用户读取和写入他们创建的任何项目领域的权限,但是对所有其他用户具有读取权限。境界。我还希望能够根据需要为其他用户分配写权限,而无需在Realm对象服务器中提供管理员状态。
我认为我的应用程序将允许用户以最小权限登录,并让第二个管理员用户在后台工作,以管理权限。管理员用户不会向用户公开。
我一直在关注https://github.com/realm-demos/realm-teamwork-MR提供的示例,但无法在设置权限方面取得任何成功。我的测试用例如下:
import UIKit
import RealmSwift
import Realm
let ApplicationName = "SyncTest"
let syncHost = "127.0.0.1" // The realm-oject-server is hosted on AWS, however changed for this example to keep user data private. HTTPS has also been implemented.
let syncAuthURL = URL(string: "https://\(syncHost):9443")!
let commonRealmURL:URL = URL(string: "realms://\(syncHost):9443/\(ApplicationName)-CommonRealm")!
class Dog: Object {
dynamic var name = ""
dynamic var age = 0
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
updateUserPermissions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func updateUserPermissions() {
// Create the callback block that will perform the request
let logInBlock: ((SyncCredentials) -> Void) = { credentials in
SyncUser.logIn(with: credentials, server: syncAuthURL, timeout: 30, onCompletion: { (user, error) in
DispatchQueue.main.async {
// Display an error message if the login failed
if let error = error {
self.showError(title: "Unable to Sign In", message: error.localizedDescription)
return
}
guard let user = user else { return }
print("ID: \(String(describing: user.identity)), Total Users Logged In: \(SyncUser.all.count)")
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: commonRealmURL), objectTypes: [Dog.self])
let adminRealm:Realm = try! Realm(configuration: config)
let permission = SyncPermissionValue(realmPath: adminRealm.configuration.syncConfiguration!.realmURL.path,
username: "user@host.com",
accessLevel: .write)
user.applyPermission(permission) { error in
if let error = error {
self.showError(title: "Unable to Apply Permissions", message: error.localizedDescription)
return
}
}
let myDog = Dog()
myDog.name = "admin" + Date().description
myDog.age = 1
try! adminRealm.write {
adminRealm.add(myDog)
}
let results = adminRealm.objects(Dog.self)
print("Number of results after admin login: \(results.count)")
self.logInUser()
}
})
}
let credentials = SyncCredentials.usernamePassword(username: "admin@host.com", password: "admin", register: false)
logInBlock(credentials)
}
private func showError(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
private func logInUser() {
// Create the callback block that will perform the request
let logInBlock: ((SyncCredentials) -> Void) = { credentials in
SyncUser.logIn(with: credentials, server: syncAuthURL, timeout: 30, onCompletion: { (user, error) in
DispatchQueue.main.async {
// Display an error message if the login failed
if let error = error {
self.showError(title: "Unable to Sign In", message: error.localizedDescription)
return
}
guard let user = user else { return }
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: commonRealmURL), objectTypes: [Dog.self])
let userRealm = try! Realm(configuration: config)
let myDog = Dog()
myDog.name = "user" + Date().description
myDog.age = 2
try! userRealm.write {
userRealm.add(myDog)
}
let results = userRealm.objects(Dog.self)
print("Number of results after user login: \(results.count)")
}
})
}
let credentials = SyncCredentials.usernamePassword(username: "user@host.com", password: "user", register: false)
logInBlock(credentials)
}
}
有关如何使用后台管理员用户成功分配权限的任何想法?或者我会更好地为我的数据库使用不同的结构?谢谢!