Beispiel #1
0
 def _get_user_field(self, username, fieldName):
     if not self.exists(username):
         raise InvalidUserError(username)
     query = "SELECT " + fieldName + " FROM users WHERE Username = ?"
     results = self._execute_query(query, (username, ))
     assert len(results) == 1
     return results[0][0]
Beispiel #2
0
    def set_repos(self, username, repoPaths):
        assert isinstance(username, str)
        if not self.exists(username):
            raise InvalidUserError(username)
        user_id = self._get_user_id(username)

        # We don't want to just delete and recreate the repos, since that
        # would lose notification information.
        existingRepos = self.get_repos(username)
        reposToDelete = [x for x in existingRepos if x not in repoPaths]
        reposToAdd = [x for x in repoPaths if x not in existingRepos]

        # delete any obsolete repos
        for repo in reposToDelete:
            query = "DELETE FROM repos WHERE UserID=? AND RepoPath=?"
            self._execute_query(query, (user_id, repo))

        # add in new repos
        query = "INSERT INTO repos (UserID, RepoPath) values (?, ?)"
        repoPaths = [[user_id, repo] for repo in reposToAdd]
        conn = self._connect()
        try:
            cursor = conn.cursor()
            cursor.executemany(query, repoPaths)
        finally:
            conn.close()
Beispiel #3
0
 def get_repos(self, username):
     """
     Get list of repos for the given `username`.
     """
     assert isinstance(username, str)
     if not self.exists(username):
         raise InvalidUserError(username)
     query = ("SELECT RepoPath FROM repos WHERE UserID = %d" %
              self._get_user_id(username))
     return [row[0] for row in self._execute_query(query)]
Beispiel #4
0
 def set_user_root(self, username, user_root):
     assert isinstance(username, str)
     assert isinstance(user_root, str)
     if not self.exists(username):
         raise InvalidUserError(username)
     # Remove the user from the cache before
     # updating the database.
     self._user_root_cache.pop(username, None)
     self._execute_query("UPDATE users SET UserRoot=? WHERE Username = ?",
                         (user_root, username))
Beispiel #5
0
 def set_is_admin(self, username, is_admin):
     assert isinstance(username, str)
     if not self.exists(username):
         raise InvalidUserError(username)
     if is_admin:
         admin_int = 1
     else:
         admin_int = 0
     self._execute_query("UPDATE users SET IsAdmin = ? WHERE Username = ?",
                         (admin_int, username))
Beispiel #6
0
    def _set_user_field(self, username, fieldName, value):
        assert isinstance(username, str)
        assert isinstance(fieldName, str)
        assert isinstance(value, str) or isinstance(value, bool) or isinstance(
            value, int)

        if not self.exists(username):
            raise InvalidUserError(username)
        if isinstance(value, bool):
            if value:
                value = '1'
            else:
                value = '0'
        query = 'UPDATE users SET ' + fieldName + '=? WHERE Username=?'
        self._execute_query(query, (value, username))
Beispiel #7
0
 def set_password(self, user, password, old_password=None):
     # Check if user exists in database
     db = self.find_user_database(user)
     if not db:
         raise InvalidUserError(user)
     # Try to update the user password.
     store = self.find_user_store(user)
     if store and not store.supports('set_password'):
         logger.warn(
             "authentication backend for user [%s] does not support changing the password",
             user)
         raise RdiffError(_("You cannot change the user's password."))
     elif not store:
         store = self._get_supporting_store('set_password')
     if not store:
         logger.warn(
             "none of the IPasswordStore supports setting the password")
         raise RdiffError(_("You cannot change the user's password."))
     store.set_password(user, password, old_password)
     self._notify('password_changed', user, password)
Beispiel #8
0
 def get_user(self, username):
     """Return a user object."""
     db = self.find_user_database(username)
     if not db:
         raise InvalidUserError(username)
     return UserObject(self, db, username)