【问题标题】:Vapor 4/Fluent, function with nested database lookup to return return EventLoopFuture<bool>Vapor 4/Fluent,具有嵌套数据库查找以返回返回 EventLoopF​​uture<bool> 的函数
【发布时间】:2020-09-22 21:17:23
【问题描述】:

我正在尝试编写一个接收我的 UserModel 的函数,并执行一些检查以查看用户是否

  1. 已锁定
  2. 在一段时间内未尝试过多登录。

然后返回结果的布尔值指示。

查找在我的身份验证过程中工作。但我想分解确定是否允许用户(尝试)登录的代码,这样我就可以在多个地方使用它而无需重复代码。

但是(刚接触 Vapor/Swift)我遇到了一个错误,我无法弄清楚我做错了什么: 无法将“EventLoopF​​uture”类型的返回表达式转换为“Bool”类型

错误在 }.all().map { 行(单独成一行,以便于查找)。

数据库结构方面,我涉及 2 个表:

  • UserAccess 保存我的用户个人资料详细信息(此用户可以进行多少次错误尝试,以及我们在日志中查找登录尝试的时间有多长)
  • UserLog,其中记录了每个用户的登录尝试情况以及他们的登录尝试时间

到目前为止,这是我的代码 sn-p:

func CanUserLogin(user: UserModel, req: Request) -> EventLoopFuture<Bool> {
  if(!(user.locked ?? false)) {
    let userProfileId = user.userprofile

    return Usertype.query(on: req.db)
      .filter(\.$profilenum == userProfileId)
      .first().map { useraccess in
        let badloginperiod = Double((useraccess!.badloginperiod ?? 0) * -1 * 60) // convert minutes to seconds (we need a negative number)
        let lookUpDate = Date().addingTimeInterval(badloginperiod)

        return Userlog.query(on: req.db).group(.and) {
          and in
          and.filter(\.$username == user.username)
          and.filter(\.$datecreated >= lookUpDate)
        }.all().map {
          UserLogs -> Bool in
          let value = userLogs.count

          // the account is locked or the max attempts for the time peroid
          if(value >= (useraccess.maxloginattempts ?? 3)) {
            return false
          } else {
            return true
          }
        }
    }
  }
}

任何方向都将不胜感激。

【问题讨论】:

标签: arrays vapor vapor-fluent


【解决方案1】:

您尝试从 map 块返回 EventLoopFuture,但您只能从中返回 non-future 值。因此,您必须在 Usertype 查询上使用 flatMap 而不是 map

查看此代码

func canUserLogin(user: UserModel, req: Request) -> EventLoopFuture<Bool> {
    guard user.locked != true else {
        return req.eventLoop.makeFailedFuture(Abort(.badRequest, reason: "User is locked"))
    }
    let userProfileId = user.userprofile
    return Usertype.query(on: req.db)
        .filter(\.$profilenum == userProfileId)
        .first()
        .unwrap(or: Abort(.forbidden, reason: "No access"))
        .flatMap { userAccess in
            let badloginperiod = Double((useraccess.badloginperiod ?? 0) * -1 * 60) // convert minutes to seconds (we need a negative number)
            let lookUpDate = Date().addingTimeInterval(badloginperiod)
            return Userlog.query(on: req.db).group(.and) {
              $0.filter(\.$username == user.username)
              $0.filter(\.$datecreated >= lookUpDate)
            }.all().map { attempts -> Bool in
                // the account is locked or the max attempts for the time peroid
                if attempts.count >= (userAccess.maxloginattempts ?? 3) {
                    return false
                } else {
                    return true
                }
            }
    }
}

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多