Exemplo n.º 1
0
    def _submit(self, ctx, form, data):

        usernameValidation(data)

        avatar = itub.IAvatar(ctx)
        sess = util.getStoreSession(ctx)

        def flush(result, sess):
            return sess.flush().addCallback(lambda spam: result)

        def userCreated(user):
            return url.URL.fromContext(ctx).sibling(user.id).replace('message', 'User added successfully')

        def duplicateUser(failure):
            failure.trap(user.UserExistsError)
            sess.forceRollback = True
            raise forms.validation.FieldValidationError( 'duplicate user', 'username' )

        def catchUnauthorizedException(failure):
            failure.trap(capabilities.UnauthorizedException)
            sess.forceRollback = True
            return url.URL.fromContext(ctx).replace('errormessage', 'You do not have permission to create users.')

        d = avatar.getUserManager(sess)
        d.addCallback(lambda userManager: userManager.createUser(**data))
        d.addErrback(duplicateUser)
        d.addCallback(userCreated)
        d.addErrback(catchUnauthorizedException)
        return d
Exemplo n.º 2
0
 def data_users(self, ctx, data):
     """Fetch and return a list of users known to the system.
     """
     avatar = itub.IAvatar(ctx)
     d = avatar.getUserManager(util.getStoreSession(ctx))
     d.addCallback(lambda userManager: userManager.findMany())
     return d
Exemplo n.º 3
0
    def child__submitDelete(self, ctx):
        # Get the list of user ids to delete from the form and
        userids = inevow.IRequest(ctx).args.get('userid')
        if not userids:
            return url.URL.fromContext(ctx)

        def removeUser(userManager, id):
            d = userManager.removeUser(id)
            d.addCallback(lambda ignore: userManager)
            return d

        def usersDeleted(spam):
            return url.URL.fromContext(ctx).replace('message', 'Users deleted successfully')

        def catchUnauthorizedException(failure):
            failure.trap(capabilities.UnauthorizedException)
            sess.forceRollback = True
            return url.URL.fromContext(ctx).replace('errormessage', 'You do not have permission to delete users.')

        avatar = itub.IAvatar(ctx)
        sess = util.getStoreSession(ctx)
        d = avatar.getUserManager(sess)
        for userid in userids:
            d.addCallback(removeUser, userid)
        d.addCallback(lambda spam: sess.flush())
        d.addCallback(usersDeleted)
        d.addErrback(catchUnauthorizedException)
        return d
Exemplo n.º 4
0
 def childFactory(self, ctx, userId):
     # User IDs are always ints
     try:
         userId = int(userId)
     except ValueError:
         return None
     def error(failure):
         failure.trap(objstore.NotFound)
         return None
     avatar = itub.IAvatar(ctx)
     d = avatar.getUserManager(util.getStoreSession(ctx))
     d.addCallback(lambda userManager: userManager.findById(userId))
     return d.addCallback(EditUserPage).addErrback(error)
Exemplo n.º 5
0
    def _submit(self, ctx, form, data):

        usernameValidation(data, self.original)

        sess = util.getStoreSession(ctx)

        def updateSuspended():
            suspended = data.get('suspended', None)
            if suspended is None or self.original.suspended == suspended:
                return
            self.original.setSuspended(suspended)

        def userUpdated(user):
#            if data.get('password', None):
#                inevow.ISession(ctx).getComponent(IGuard).updateCredentials(ctx, self.original.username, data.get('password'))

            return url.URL.fromContext(ctx).replace('message', 'User updated successfully')

        def updateFailed(failure):
            failure.trap(NotFound)
            util.getStoreSession(ctx).forceRollback = True
            return url.URL.fromContext(ctx).replace('errormessage', 'User update failed. Someone else has already changed the user.')

        def catchUnauthorizedException(failure):

            failure.trap(capabilities.UnauthorizedException)
            sess.forceRollback = True
            return url.URL.fromContext(ctx).replace('errormessage', 'You do not have permission to update the details of this user.')

        d = defer.Deferred()
        d.addCallback(lambda ignore: self.original.update(data))
        d.addCallback(lambda ignore: updateSuspended())
        d.addCallback(lambda ignore: sess.flush())
        d.addCallback(userUpdated)
        d.addErrback(updateFailed)
        d.addErrback(catchUnauthorizedException)
        d.callback(None)
        return d
Exemplo n.º 6
0
 def updateFailed(failure):
     failure.trap(NotFound)
     util.getStoreSession(ctx).forceRollback = True
     return url.URL.fromContext(ctx).replace('errormessage', 'User update failed. Someone else has already changed the user.')