Exemplo n.º 1
0
 def ReadGRRUser(self, username):
     """Reads a user object corresponding to a given name."""
     try:
         u = self.users[username]
         return rdf_objects.GRRUser(username=u["username"],
                                    password=u.get("password"),
                                    ui_mode=u.get("ui_mode"),
                                    canary_mode=u.get("canary_mode"),
                                    user_type=u.get("user_type"))
     except KeyError:
         raise db.UnknownGRRUserError("Can't find user with name: %s" %
                                      username)
Exemplo n.º 2
0
    def WriteUserNotification(self, notification):
        """Writes a notification for a given user."""
        if notification.username not in self.users:
            raise db.UnknownGRRUserError("User %s not found!" %
                                         notification.username)

        cloned_notification = notification.Copy()
        if not cloned_notification.timestamp:
            cloned_notification.timestamp = rdfvalue.RDFDatetime.Now()

        self.notifications_by_username.setdefault(
            cloned_notification.username, []).append(cloned_notification)
Exemplo n.º 3
0
    def ReadGRRUser(self, username, cursor=None):
        """Reads a user object corresponding to a given name."""
        cursor.execute(
            "SELECT username, password, ui_mode, canary_mode, user_type "
            "FROM grr_users WHERE username_hash = %s",
            [mysql_utils.Hash(username)])

        row = cursor.fetchone()
        if row is None:
            raise db.UnknownGRRUserError(username)

        return self._RowToGRRUser(row)
Exemplo n.º 4
0
  def DeleteGRRUser(self, username):
    """Deletes the user and all related metadata with the given username."""
    try:
      del self.approvals_by_username[username]
    except KeyError:
      pass  # No approvals to delete for this user.

    for approvals in itervalues(self.approvals_by_username):
      for approval in itervalues(approvals):
        grants = [g for g in approval.grants if g.grantor_username != username]
        if len(grants) != len(approval.grants):
          approval.grants = grants

    try:
      del self.notifications_by_username[username]
    except KeyError:
      pass  # No notifications to delete for this user.

    try:
      del self.users[username]
    except KeyError:
      raise db.UnknownGRRUserError(username)
Exemplo n.º 5
0
    def WriteUserNotification(self, notification, cursor=None):
        """Writes a notification for a given user."""
        # Copy the notification to ensure we don't modify the source object.
        notification = notification.Copy()

        if not notification.timestamp:
            notification.timestamp = rdfvalue.RDFDatetime.Now()

        query = ("INSERT INTO user_notification (username, timestamp, "
                 "notification_state, notification) "
                 "VALUES (%s, %s, %s, %s)")

        args = [
            notification.username,
            mysql_utils.RDFDatetimeToMysqlString(notification.timestamp),
            int(notification.state),
            notification.SerializeToString()
        ]
        try:
            cursor.execute(query, args)
        except MySQLdb.IntegrityError:
            raise db.UnknownGRRUserError(notification.username)
Exemplo n.º 6
0
 def DeleteGRRUser(self, username):
     """Deletes the user with the given username."""
     try:
         del self.users[username]
     except KeyError:
         raise db.UnknownGRRUserError(username)
Exemplo n.º 7
0
 def ReadGRRUser(self, username):
     """Reads a user object corresponding to a given name."""
     try:
         return self.users[username].Copy()
     except KeyError:
         raise db.UnknownGRRUserError(username)
Exemplo n.º 8
0
 def SampleCallWithGRRError(self):
     raise db.UnknownGRRUserError()