Пример #1
0
    def action_add_users(self, resource, context, form):
        new_users = form['new_users'].strip()
        users = resource.get_resource('/users')
        root = context.root
        added = []
        for lineno, line in enumerate(new_users.splitlines()):
            lastname, email = parseaddr(line)
            try:
                email = email.encode('utf-8')
            except UnicodeEncodeError:
                email = None
            if not email or not EmailField.is_valid(email):
                context.commit = False
                message = ERROR(u"Unrecognized line {lineno}: {line}")
                context.message = message.gettext(lineno=lineno + 1, line=line)
                return
            if type(lastname) is str:
                lastname = unicode(lastname)
            # Is the user already known?
            user = root.get_user_from_login(email)
            if user is None:
                # Register the user
                user = users.set_user(**{'email': email, 'lastname': lastname})
            resource.subscribe_user(user)
            added.append(user.name)

        if not added:
            context.message = ERROR(u"No user added.")
            return

        context.body['new_users'] = u""

        message = INFO(u"{n} user(s) added.")
        context.message = message.gettext(n=len(added))
Пример #2
0
    def _get_form(self, resource, context):
        # We cannot call direct parent NewInstance, because we override the
        # container
        form = super(NewInstance, self)._get_form(resource, context)

        # 1. The container
        container = self._get_container(resource, context)
        ac = container.get_access_control()
        if not ac.is_allowed_to_add(context.user, container):
            path = context.get_link(container)
            path = '/' if path == '.' else '/%s/' % path
            msg = ERROR(u'Adding resources to {path} is not allowed.')
            raise FormError, msg.gettext(path=path)

        # 2. Strip the title
        form['title'] = form['title'].strip()

        # 3. The name
        name = self.get_new_resource_name(form)
        if not name:
            raise FormError, messages.MSG_NAME_MISSING
        try:
            name = checkid(name)
        except UnicodeEncodeError:
            name = None
        if name is None:
            raise FormError, messages.MSG_BAD_NAME
        # Check the name is free
        if container.get_resource(name, soft=True) is not None:
            raise FormError, messages.MSG_NAME_CLASH
        form['name'] = name

        # Ok
        return form
Пример #3
0
    def get_container(self, resource, context, form):
        # Container
        container = resource
        path = str(container.abspath)

        # Access control
        class_id = context.query['type']
        root = context.root
        if not root.has_permission(context.user, 'add', container, class_id):
            path = '/' if path == '.' else '/%s/' % path
            msg = ERROR(u'Adding resources to {path} is not allowed.')
            raise FormError, msg.gettext(path=path)

        # Ok
        return container
Пример #4
0
    def get_container(self, resource, context, form):
        # Container
        container = resource
        path = str(container.abspath)

        # Access control
        class_id = context.query['type']
        root = context.root
        if not root.has_permission(context.user, 'add', container, class_id):
            path = '/' if path == '.' else '/%s/' % path
            msg = ERROR(u'Adding resources to {path} is not allowed.')
            raise FormError, msg.gettext(path=path)

        # Ok
        return container
Пример #5
0
    def get_container(self, resource, context, form):
        # Container
        container = resource
        if 'location' in self.get_fields():
            path = form['location']
            if path is not None:
                container = resource.get_resource(path)

        # Access control
        class_id = context.query['type']
        root = context.root
        if not root.has_permission(context.user, 'add', container, class_id):
            path = '/' if path == '.' else '/%s/' % path
            msg = ERROR(u'Adding resources to {path} is not allowed.')
            raise FormError, msg.gettext(path=path)

        # Ok
        return container