/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import $ from 'jquery';
import _ from 'underscore';
import React from 'react';
import Permissions from './permissions';
import PermissionsFooter from './permissions-footer';
import Search from './search';
import ApplyTemplateView from './apply-template-view';
import { translate } from '../../helpers/l10n';
import { TooltipsContainer } from '../../components/mixins/tooltips-mixin';
import '../permission-templates/styles.css';
const PERMISSIONS_ORDER = ['user', 'codeviewer', 'issueadmin', 'admin', 'scan'];
export default React.createClass({
propTypes: {
permissionTemplates: React.PropTypes.arrayOf(React.PropTypes.object).isRequired
},
getInitialState() {
return { ready: false, permissions: [], projects: [], total: 0, filter: '__ALL__' };
},
componentDidMount() {
this.requestPermissions();
},
sortPermissions(permissions) {
return _.sortBy(permissions, p => PERMISSIONS_ORDER.indexOf(p.key));
},
mergePermissionsToProjects(projects, basePermissions) {
return projects.map(project => {
// it's important to keep the order of the project permissions the same as the order of base permissions
const permissions = basePermissions.map(basePermission => {
const projectPermission = _.findWhere(project.permissions, { key: basePermission.key });
return _.extend({ usersCount: 0, groupsCount: 0 }, basePermission, projectPermission);
});
return _.extend({}, project, { permissions });
});
},
requestPermissions(page = 1, query = '', filter = this.state.filter) {
const url = window.baseUrl + '/api/permissions/search_project_permissions';
let data = { p: page, q: query };
if (filter !== '__ALL__') {
data.qualifier = filter;
}
if (this.props.componentId) {
data = { projectId: this.props.componentId };
}
this.setState({ ready: false }, () => {
$.get(url, data).done(r => {
const permissions = this.sortPermissions(r.permissions);
let projects = this.mergePermissionsToProjects(r.projects, permissions);
if (page > 1) {
projects = [].concat(this.state.projects, projects);
}
this.setState({
ready: true,
projects,
permissions,
total: r.paging.total,
page: r.paging.pageIndex,
query,
filter
});
});
});
},
loadMore() {
this.requestPermissions(this.state.page + 1, this.state.query);
},
search(query) {
this.requestPermissions(1, query);
},
handleFilter(filter) {
this.requestPermissions(1, this.state.query, filter);
},
refresh() {
this.requestPermissions(1, this.state.query);
},
bulkApplyTemplate(e) {
e.preventDefault();
new ApplyTemplateView({
query: this.state.query,
filter: this.state.filter,
permissionTemplates: this.props.permissionTemplates,
refresh: () => this.requestPermissions(1, this.state.query, this.state.filter)
}).render();
},
renderBulkApplyButton() {
if (this.props.componentId) {
return null;
}
return (
);
},
renderSpinner () {
if (this.state.ready) {
return null;
}
return ;
},
render() {
return (