如何在社交媒体应用中阻止Firebase上的用户?对于iOS

时间:2018-05-02 16:53:18

标签: swift firebase firebase-security

enter image description here我使用我的应用程序约90%,并准备发布它以便可以发布以进行测试。

我被苹果拒绝,因为我没有一个非常重要的功能: - 用户阻止滥用用户的机制。

我已经有了一个跟随其他用户的功​​能,但我仍然坚持如何阻止访问,这样当一个用户阻止另一个用户时,他们看不到任何与阻止他的人相关的内容。他们无法在搜索,新消息和帖子中看到他们的个人资料。

我不知道从哪里开始。如果您使用过用户内容生成的应用程序。我甚至不知道从哪里开始。我已经坚持了3个星期,现在我很绝望。

//我将如何实现这样的东西?

我的安全规则是基本的。

{
  "rules": {
".read": "auth != null",
".write": "auth != null"

} }

我显示信息的方式,就像现在一样,用户必须使用电子邮件和用户名和密码登录,并创建'和用户,允许他们登录,进入应用程序并查看相关的所有内容到应用程序。

我的节点

Messages

评论 标记短柱 以下 喜欢 帖子 用户的消息 用户

3 个答案:

答案 0 :(得分:4)

这个问题有很多解决方案。一种方法是在数据库中为被阻止的用户提供单独的节点。在每个用户下,您可以列出被阻止用户的uid,其值为" true"。这个值是什么并不重要 - 这样可以更容易地搜索被阻止的人。因此,例如,假设这是您的数据库结构:

users:
    uid1:
       my data: {
           // some stuff here
       }
    uid2:
       my data: {
           // some stuff here
       }
blocked:
     uid1:
         uid8: true,
         uid3: true
     uid2:
         uid1: true

在上面的示例中,uid1已屏蔽uid8uid3,依此类推。 然后,您可以调整规则以验证用户是否可以阅读是否经过身份验证,并且在该用户的阻止列表中找不到uid

{
  "rules": {
    "users": {
      "$user_id": {
        // only messages from the last ten minutes can be read
        ".read": "auth != null && !root.child('blocked/'+$user_id+'/'+auth.uid+'/true').exists()",
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

有关安全规则的一些示例,请查看有关安全规则的documentation

答案 1 :(得分:0)

我认为你的数据库中的某个地方有一个用户模型正确吗?只需添加另一个名为isBlocked或其他的字段,根据该值,您可以验证某些用户是否可以看到该用户

答案 2 :(得分:0)

这是我想出的,对我来说很好用。 这与instagram一样,被阻止的用户完全从您的供稿中消失了,但这肯定会使Apple陷入困境,因为一个用户可以阻止另一个用户,一旦发生这种情况互相发送消息。

它还为您提供了显示操作单时显示“阻止”或“取消阻止”的选项,以便当前用户知道他们是否阻止了另一用户。

第一件事是,我假设它在某种带有发送消息按钮的聊天视图控制器内,并且在该聊天vc内有发送者的ID(当前用户)和接收者的ID(其他用户)。

要阻止某人,您可以在Firebase中创建一个blocked引用,为要阻止另一用户的用户创建一个节点,然后最后将被阻止的用户添加为该节点下的键/值对。 other user's idkey,其值可以为true。

例如 dog123 想要阻止 cat456 。当狗决定挡住猫时,挡住的裁判将是:

let dogId = dog123 // current user
let catId = cat456 // other user

func actionSheetBlockedAction() {

    let dictValues = [String: Any]()
    dictValues.updateValue(true, forKey: catId)

    let ref = Database.database().reference().child("blocked").child(dogId)
    ref.updateChildValues(dictValues)
}

这将导致数据库如下所示:

root
  |
  @---blocked
         |
         |
         @----dog123
                |
                |----cat456: true  // cat123 is blocked from sending messages to dog123

在同一聊天室vc中,您需要添加一些属性,在viewDidLoad或viewWillAppear中,您需要添加2个单独的观察者,以监听两个用户的阻止引用。

您必须对两个用户都进行检查,因为是的,dog123可能已阻止cat456,但作为回报,cat456可能也已阻止了dog123。稍后,如果狗决定解除对猫的阻止,但猫仍然阻止了它,则消息仍会从狗传递到猫。

为防止添加观察者,观察者将在按下发送按钮时检查的某些属性上切换。如果两个属性中的任何一个为true,则表示有人阻止了其他人,并且不应发送任何消息。这些属性将使您知道谁阻止了谁。

您还可以使用这些属性在操作表出现时显示“阻止”或“取消阻止”按钮。

var currentUserId = Auth.auth().currentUser?.uid
var otherUserId = "whatever" // you should already have this
var isCurrentUserBlocked = false
var isOtherUserBlocked = false

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    checkBlockedRefsForBothUsers() // this is where the isCurrentUserBlocked & isOtherUserBlocked properties get changed
}


@IBAction func sendMessageButtonTapped(sender: UIButton) {

    if isOtherUserBlocked {
        // alert the current user that they blocked the other user
        return
    }

    if isCurrentUserBlocked {
        // alert the current user that the other user blocked them
        return
    }

    // if neither are true then let the current user send their message to the other user
}


func checkBlockedRefsForBothUsers() {

    let currentUsersBlockedRef = Database.database().reference().child("blocked").child(currentUserId!)
    currentUsersBlockedRef?.observe( .value, with: { (snapshot) in

        // if the current user ISN'T under the blocked ref then the other user ISN'T blocked
        if !snapshot.exists() {
            self.isOtherUserBlocked = false
            return
        }

        for child in snapshot.children {
            let snap = child as! DataSnapshot

            // if the other user's uid IS under the current user's blocked ref then the other user IS blocked from them
            if snap.key == self.otherUsersId {
                self.isOtherUserBlocked = true
                break
            }

             // if the other user's uid ISN'T under the current user's blocked ref then the other user ISN'T blocked
            self.isOtherUserBlocked = false
        }
    })

    let otherUsersBlockedRef = Database.database().reference().child("blocked").child(otherUserId)
    otherUsersBlockedRef?.observe( .value, with: { (snapshot) in

        // if the other user's uid ISN'T under the blocked ref then the current user ISN'T blocked
        if !snapshot.exists() {
            self.isCurrentUserBlocked = false
            return
        }

        for child in snapshot.children {

            let snap = child as! DataSnapshot

            // if the current user's uid IS under the other user's blocked ref then the current user IS blocked from them
            if snap.key == self.currentUserId {
                self.isCurrentUserBlocked = true
                break
            }

             // if the current user's uid ISN'T under the other user's blocked ref then the current user ISN'T blocked
            self.isCurrentUserBlocked = false
        }
    })
}

要在展示操作表时显示“阻止”或“取消阻止”按钮,请使用以下两个相同属性:

@IBAction func presentActionSheet(sender: UIButton) {

    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let blockAction = UIAlertAction(title: "Block", style: .default) { (action) in
         // run code to block the otherUserId
         self.actionSheetBlockedAction()
    }

    let unblockAction = UIAlertAction(title: "Unblock", style: .default) { (action) in
        // run code to unblock the otherUserId
    }

    if isOtherUserBlocked {
        actionSheet.addAction(unblockAction) // if the current user blocked the other user then show the "unblock" action
    } else {
        actionSheet.addAction(blockAction)  // if the current user didn't block the other user then show the "block" action
    }

    present(actionSheet, animated: true, completion: nil)
}

最后,您可能会想自己,当狗挡住猫时,只需更新它们的裁判即可。问题在于,当展示猫的动作表时,按照上面的逻辑,这似乎表明猫阻塞了狗,而猫的动作表将显示“取消阻止”。