From 7d6846d0a207097fab28a2c6584a6e11ff75ca50 Mon Sep 17 00:00:00 2001
From: Piero Toffanin
Date: Sun, 29 Sep 2024 14:14:48 -0400
Subject: [PATCH] Hide Add Project button when user doesn't have permissions
---
app/static/app/js/Dashboard.jsx | 24 +++++++++++++++++-------
app/templates/app/dashboard.html | 6 +++++-
app/views/app.py | 11 +++++++++--
3 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/app/static/app/js/Dashboard.jsx b/app/static/app/js/Dashboard.jsx
index 3c1544444..e4a2283f6 100644
--- a/app/static/app/js/Dashboard.jsx
+++ b/app/static/app/js/Dashboard.jsx
@@ -1,4 +1,5 @@
import React from 'react';
+import PropTypes from 'prop-types';
import './css/Dashboard.scss';
import ProjectList from './components/ProjectList';
import EditProjectDialog from './components/EditProjectDialog';
@@ -11,9 +12,16 @@ import $ from 'jquery';
import { _ } from './classes/gettext';
class Dashboard extends React.Component {
- constructor(){
- super();
-
+ static defaultProps = {
+ permissions: []
+ };
+ static propTypes = {
+ permissions: PropTypes.array.isRequired,
+ };
+
+ constructor(props){
+ super(props);
+
this.handleAddProject = this.handleAddProject.bind(this);
this.addNewProject = this.addNewProject.bind(this);
}
@@ -58,14 +66,15 @@ class Dashboard extends React.Component {
return (
+ {this.props.permissions.indexOf("add_project") !== -1 ?
-
{_("Add Project")}
-
+
: ""}
, $(this).get(0));
+ let props = $(this).data();
+ delete(props.dashboard);
+ window.ReactDOM.render( , $(this).get(0));
});
-
// Warn users if there's any sort of work in progress before
// they press the back button on the browser
// Yes it's a hack. No we're not going to track state in React just
diff --git a/app/templates/app/dashboard.html b/app/templates/app/dashboard.html
index 106998b35..fb162dd3e 100644
--- a/app/templates/app/dashboard.html
+++ b/app/templates/app/dashboard.html
@@ -43,7 +43,11 @@ {% trans 'Welcome!' %} ☺
{% endif %}
-
+
{% endif %}
{% endblock %}
diff --git a/app/views/app.py b/app/views/app.py
index 4256234a0..f909d575a 100644
--- a/app/views/app.py
+++ b/app/views/app.py
@@ -41,13 +41,20 @@ def dashboard(request):
no_tasks = Task.objects.filter(project__owner=request.user).count() == 0
no_projects = Project.objects.filter(owner=request.user).count() == 0
+ permissions = []
+ if request.user.has_perm('app.add_project'):
+ permissions.append('add_project')
+
# Create first project automatically
- if no_projects and request.user.has_perm('app.add_project'):
+ if no_projects and 'add_project' in permissions:
Project.objects.create(owner=request.user, name=_("First Project"))
return render(request, 'app/dashboard.html', {'title': _('Dashboard'),
'no_processingnodes': no_processingnodes,
- 'no_tasks': no_tasks
+ 'no_tasks': no_tasks,
+ 'params': {
+ 'permissions': json.dumps(permissions)
+ }.items()
})