Wrapping function.

def SecurityCheck(self, func, request, *args, **kwargs):
    """Wrapping function."""
    request.user = u""

    authorized = False
    try:
      auth_type, authorization = request.headers.get("Authorization",
                                                     " ").split(" ", 1)

      if auth_type == "Basic":
        authorization_string = authorization.decode("base64").decode("utf-8")
        user, password = authorization_string.split(":", 1)

        if data_store.AFF4Enabled():

          token = access_control.ACLToken(username=user)

          fd = aff4.FACTORY.Open(
              "aff4:/users/%s" % user,
              aff4_type=aff4_users.GRRUser,
              token=token)
          crypted_password = fd.Get(fd.Schema.PASSWORD)
          if crypted_password and crypted_password.CheckPassword(password):
            authorized = True

            # The password is ok - update the user
            request.user = user

        else:

          try:
            user_obj = data_store.REL_DB.ReadGRRUser(user)
            if user_obj.password.CheckPassword(password):
              authorized = True

              # The password is ok - update the user
              request.user = user

          except db.UnknownGRRUserError:
            pass

    except access_control.UnauthorizedAccess as e:
      logging.warning("UnauthorizedAccess: %s for %s", e, request)
    except (IndexError, KeyError, IOError):
      pass

    if not authorized:
      result = werkzeug_wrappers.Response("Unauthorized", status=401)
      result.headers["WWW-Authenticate"] = "Basic realm='Secure Area'"
      return result

    # Modify this to implement additional checking (e.g. enforce SSL).
    return func(request, *args, **kwargs)