Beispiel #1
0
    def _apply_changes(self, req, project):
        """
        Saves changes into database and project configuration file
        """
        try:
            # Save information into database
            project.project_name = req.args.get('name')
            project.description = req.args.get('descr')

            # Update author if needed
            author_id = req.args.get('author_id')
            if author_id and project.author_id != int(author_id):
                userstore = get_userstore()
                author = userstore.getUserWhereId(author_id)
                project.author = author

                # Check if author has admin permission to project: put in project if not
                authorperm = PermissionCache(self.env, author.username)
                if 'TRAC_ADMIN' not in authorperm:
                    admin_rights = False
                    groupstore = CQDEUserGroupStore(
                        project.trac_environment_key)
                    # Iterate existing project groups and put user into group with TRAC_ADMIN rights
                    for gname, pname in groupstore.get_all_group_permissions():
                        if pname == 'TRAC_ADMIN':
                            groupstore.add_user_to_group(
                                author.username, gname)
                            admin_rights = True
                            add_notice(
                                req,
                                _('Added TRAC_ADMIN permissions to user: {0}'.
                                  format(author.username)))

                    if not admin_rights:
                        permlink = tag.a(
                            'You way want to modify permissions',
                            href=req.href('admin/general/permissions'))
                        add_warning(
                            req,
                            tag(
                                _('User {0} does not have administrative rights to project. '
                                  .format(author.username)), permlink))

            # Save changes to database
            project.save()

            # Save information into config
            for option in ('name', 'descr'):
                self.config.set('project', option, req.args.get(option))
            self.config.save()

        except Exception, e:
            self.log.exception('Failed to save project changes')
            add_warning(req, _('Failed to save changes: {0}'.format(e)))
            return req.redirect(req.href('admin/general/basics'))
 def _make_public(self, req, project):
     cmd = MakeProjectPublic(project)
     if cmd.do():
         # Notify listeners
         for listener in self.project_change_listeners:
             listener.project_set_public(project)
         # Notify user
         add_notice(req,
                    tag(_("Project published: "), _('public groups added')))
     else:
         add_warning(req, "Failed to publish project")
 def _make_private(self, req, project):
     cmd = MakeProjectPublic(project)
     if cmd.undo():
         # Notify listeners
         for listener in self.project_change_listeners:
             listener.project_set_private(project)
         # Notify user
         add_notice(req, tag(
             _("Unpublished project: "), _('public groups removed')
         ))
     else:
         add_warning(req, "Failed to unpublish project")
 def _make_public(self, req, project):
     cmd = MakeProjectPublic(project)
     if cmd.do():
         # Notify listeners
         for listener in self.project_change_listeners:
             listener.project_set_public(project)
         # Notify user
         add_notice(req, tag(
             _("Project published: "), _('public groups added')
         ))
     else:
         add_warning(req, "Failed to publish project")
 def _make_private(self, req, project):
     cmd = MakeProjectPublic(project)
     if cmd.undo():
         # Notify listeners
         for listener in self.project_change_listeners:
             listener.project_set_private(project)
         # Notify user
         add_notice(
             req, tag(_("Unpublished project: "),
                      _('public groups removed')))
     else:
         add_warning(req, "Failed to unpublish project")
Beispiel #6
0
    def remove_user(self, req):
        """
        Show removal form and handle POST as remove action
        """
        username = req.args.get('username')

        # Check method and permissions
        if not req.method.upper() == 'POST' or not username:
            raise PermissionError()

        # Load user
        userstore = get_userstore()
        user = userstore.getUser(req.authname)
        account = userstore.getUser(username)

        if not account:
            add_warning(req, "Could not find user '{0}' from service".format(account.username))
            return req.redirect(req.href('admin/users/manage'))

        # Check permissions
        req.perm.require('USER_AUTHOR', Resource('user', id=account.id))

        # If removable user is project author, change the ownership to person who deletes the user
        papi = projects.Projects()
        for project in papi.get_authored_projects(account):
            project.author = user
            project.save()

            # Check if user has TRAC_ADMIN rights for the new project, if not, try setting
            if not req.perm.has_permission('TRAC_ADMIN', Resource('project', id=project.id)):
                groupstore = CQDEUserGroupStore(project.trac_environment_key)
                # Iterate existing project groups and put user into group with TRAC_ADMIN rights
                for gname, pname in groupstore.get_all_group_permissions():
                    if pname == 'TRAC_ADMIN':
                        groupstore.add_user_to_group(project.author.username, gname)
                        self.log.info('Added TRAC_ADMIN permissions to {0} at {0}'.format(project.author, project))

            self.log.info('Changed ownership of project {0} from {0} to {0}'.format(project, project.author, user))
            add_notice(req, tag(_("Changed ownership of the project to you: "), tag.a(project.project_name, href=req.href('..', project.env_name))))

        if userstore.deleteUser(account):
            add_notice(req, "Removed user '{0}' successfully from local store".format(account.username))
        else:
            add_warning(req, "Failed to remove user '{0}' from local store".format(account.username))

        # Redirect to user listing
        return req.redirect(req.href('admin/users/manage'))
Beispiel #7
0
    def remove_user(self, req):
        """
        Show removal form and handle POST as remove action
        """
        username = req.args.get('username')

        # Check method and permissions
        if not req.method.upper() == 'POST' or not username:
            raise PermissionError()

        # Load user
        userstore = get_userstore()
        user = userstore.getUser(req.authname)
        account = userstore.getUser(username)

        if not account:
            add_warning(req, "Could not find user '{0}' from service".format(account.username))
            return req.redirect(req.href('admin/users/manage'))

        # Check permissions
        req.perm.require('USER_AUTHOR', Resource('user', id=account.id))

        # If removable user is project author, change the ownership to person who deletes the user
        papi = projects.Projects()
        for project in papi.get_authored_projects(account):
            project.author = user
            project.save()

            # Check if user has TRAC_ADMIN rights for the new project, if not, try setting
            if not req.perm.has_permission('TRAC_ADMIN', Resource('project', id=project.id)):
                groupstore = CQDEUserGroupStore(project.trac_environment_key)
                # Iterate existing project groups and put user into group with TRAC_ADMIN rights
                for gname, pname in groupstore.get_all_group_permissions():
                    if pname == 'TRAC_ADMIN':
                        groupstore.add_user_to_group(project.author.username, gname)
                        self.log.info('Added TRAC_ADMIN permissions to {0} at {0}'.format(project.author, project))

            self.log.info('Changed ownership of project {0} from {0} to {0}'.format(project, project.author, user))
            add_notice(req, tag(_("Changed ownership of the project to you: "), tag.a(project.project_name, href=req.href('..', project.env_name))))

        if userstore.deleteUser(account):
            add_notice(req, "Removed user '{0}' successfully from local store".format(account.username))
        else:
            add_warning(req, "Failed to remove user '{0}' from local store".format(account.username))

        # Redirect to user listing
        return req.redirect(req.href('admin/users/manage'))
Beispiel #8
0
    def _apply_changes(self, req, project):
        """
        Saves changes into database and project configuration file
        """
        try:
            # Save information into database
            project.project_name = req.args.get('name')
            project.description = req.args.get('descr')

            # Update author if needed
            author_id = req.args.get('author_id')
            if author_id and project.author_id != int(author_id):
                userstore = get_userstore()
                author = userstore.getUserWhereId(author_id)
                project.author = author

                # Check if author has admin permission to project: put in project if not
                authorperm = PermissionCache(self.env, author.username)
                if 'TRAC_ADMIN' not in authorperm:
                    admin_rights = False
                    groupstore = CQDEUserGroupStore(project.trac_environment_key)
                    # Iterate existing project groups and put user into group with TRAC_ADMIN rights
                    for gname, pname in groupstore.get_all_group_permissions():
                        if pname == 'TRAC_ADMIN':
                            groupstore.add_user_to_group(author.username, gname)
                            admin_rights = True
                            add_notice(req, _('Added TRAC_ADMIN permissions to user: {0}'.format(author.username)))

                    if not admin_rights:
                        permlink = tag.a('You way want to modify permissions', href=req.href('admin/general/permissions'))
                        add_warning(req, tag(_('User {0} does not have administrative rights to project. '.format(author.username)), permlink))

            # Save changes to database
            project.save()

            # Save information into config
            for option in ('name', 'descr'):
                self.config.set('project', option, req.args.get(option))
            self.config.save()

        except Exception, e:
            self.log.exception('Failed to save project changes')
            add_warning(req, _('Failed to save changes: {0}'.format(e)))
            return req.redirect(req.href('admin/general/basics'))
Beispiel #9
0
    def render_admin_panel(self, req, cat, page, path_info):
        """ Renders admin panel and handles new user creation request
        """
        req.perm.require('USER_CREATE')

        now = datetime.utcnow()
        expires = now + timedelta(days=90)
        data = {
            'dateformats': DATEFORMATS,
            'now': now,
            'expires': expires,
        }
        # Helper class
        add_script(req, 'multiproject/js/multiproject.js')
        add_script(req, 'multiproject/js/admin_user_create.js')

        # Get and set option goto address
        if 'goto' in req.args:
            req.session['goto'] = conf.safe_address(req.args.get('goto', ''))
            req.session.save()

        # Create new user to local database
        if req.method.upper() == 'GET':
            return 'admin_user_create.html', data

        elif req.method.upper() == 'POST':
            userstore = get_userstore()
            user = self._get_user(req)
            author = userstore.getUser(req.authname)

            # Update data for pre-filled form
            data['username'] = user.username
            data['first'] = user.givenName
            data['last'] = user.lastName
            data['mail'] = user.mail
            data['mobile'] = user.mobile

            # Validate and set author
            if not req.perm.has_permission('USER_AUTHOR') or not author:
                chrome.add_warning(
                    req,
                    _("User needs to have author with USER_AUTHOR permission"))
                return 'admin_user_create.html', data
            user.author_id = author.id
            user.expires = expires

            org_store = CQDEOrganizationStore.instance()
            auth_store = CQDEAuthenticationStore.instance()
            user.authentication_key = auth_store.get_authentication_id(
                LocalAuthentication.LOCAL)
            user.organization_keys = org_store.get_organization_keys(
                user, LocalAuthentication.LOCAL) or None

            # Validate user object
            error_msg = self.validate_user(req, user)
            if error_msg:
                chrome.add_warning(req, error_msg)
                return 'admin_user_create.html', data

            # Try to store user
            if userstore.storeUser(user):
                userlink = tag.a(user.username,
                                 href=req.href('admin/users/manage',
                                               username=user.username))
                chrome.add_notice(req,
                                  tag(_('Created new local user: '******'Created new local user "%s" by "%s"' %
                              (user.username, req.authname))

                # Try to send email notification also
                try:
                    self._send_notification(user, req.server_name)
                except TracError:
                    # Notification sending failed
                    self.log.exception("Notification sending failed")
                    chrome.add_warning(req,
                                       _('Failed to send email notification'))

                # Handle optional goto argument
                if 'goto' in req.session:
                    goto = req.session['goto']
                    del req.session['goto']

                    # NOTE: Show redirect address as a system message instead of direct redirection
                    # This is because after moving to another project, the system messages are not shown due the separate
                    # sessions per project
                    chrome.add_notice(
                        req,
                        Markup('Go back to: <a href="%s">%s</a>' %
                               (goto, goto)))

                # Redirect to the page so that we're not showing the created user form with prefilled
                return req.redirect(req.href('admin/users/create_local'))

            return 'admin_user_create.html', data
Beispiel #10
0
    def render_admin_panel(self, req, cat, page, path_info):
        """ Renders admin panel and handles new user creation request
        """
        req.perm.require('USER_CREATE')

        now = datetime.utcnow()
        expires = now + timedelta(days=90)
        data = {
            'dateformats':DATEFORMATS,
            'now':now,
            'expires':expires,
        }
        # Helper class
        add_script(req, 'multiproject/js/multiproject.js')
        add_script(req, 'multiproject/js/admin_user_create.js')

        # Get and set option goto address
        if 'goto' in req.args:
            req.session['goto'] = conf.safe_address(req.args.get('goto', ''))
            req.session.save()

        # Create new user to local database
        if req.method.upper() == 'GET':
            return 'admin_user_create.html', data

        elif req.method.upper() == 'POST':
            userstore = get_userstore()
            user = self._get_user(req)
            author = userstore.getUser(req.authname)

            # Update data for pre-filled form
            data['username'] = user.username
            data['first'] = user.givenName
            data['last'] = user.lastName
            data['mail'] = user.mail
            data['mobile'] = user.mobile

            # Validate and set author
            if not req.perm.has_permission('USER_AUTHOR') or not author:
                chrome.add_warning(req, _("User needs to have author with USER_AUTHOR permission"))
                return 'admin_user_create.html', data
            user.author_id = author.id
            user.expires = expires

            org_store = CQDEOrganizationStore.instance()
            auth_store = CQDEAuthenticationStore.instance()
            user.authentication_key = auth_store.get_authentication_id(LocalAuthentication.LOCAL)
            user.organization_keys = org_store.get_organization_keys(user, LocalAuthentication.LOCAL) or None

            # Validate user object
            error_msg = self.validate_user(req, user)
            if error_msg:
                chrome.add_warning(req, error_msg)
                return 'admin_user_create.html', data

            # Try to store user
            if userstore.storeUser(user):
                userlink = tag.a(user.username, href=req.href('admin/users/manage', username=user.username))
                chrome.add_notice(req, tag(_('Created new local user: '******'Created new local user "%s" by "%s"' % (user.username, req.authname))

                # Try to send email notification also
                try:
                    self._send_notification(user)
                except TracError:
                    # Notification sending failed
                    self.log.exception("Notification sending failed")
                    chrome.add_warning(req, _('Failed to send email notification'))

                # Handle optional goto argument
                if 'goto' in req.session:
                    goto = req.session['goto']
                    del req.session['goto']

                    # NOTE: Show redirect address as a system message instead of direct redirection
                    # This is because after moving to another project, the system messages are not shown due the separate
                    # sessions per project
                    chrome.add_notice(req, Markup('Go back to: <a href="%s">%s</a>' % (goto, goto)))

                # Redirect to the page so that we're not showing the created user form with prefilled
                return req.redirect(req.href('admin/users/create_local'))

            return 'admin_user_create.html', data