Limit the number of owners per project

This commit is contained in:
Ben Visness 2021-12-08 22:23:20 -06:00
parent e70dcbdd53
commit 5344e9d4bc
2 changed files with 19 additions and 0 deletions

View File

@ -236,6 +236,8 @@
const OWNER_QUERY_STATE_IDLE = 0; const OWNER_QUERY_STATE_IDLE = 0;
const OWNER_QUERY_STATE_QUERYING = 1; const OWNER_QUERY_STATE_QUERYING = 1;
const MAX_OWNERS = {{ .MaxOwners }};
let ownerCheckUrl = "{{ .APICheckUsernameUrl }}"; let ownerCheckUrl = "{{ .APICheckUsernameUrl }}";
let ownerQueryState = OWNER_QUERY_STATE_IDLE; let ownerQueryState = OWNER_QUERY_STATE_IDLE;
let addOwnerInput = document.querySelector("#owner_name"); let addOwnerInput = document.querySelector("#owner_name");
@ -256,6 +258,12 @@
startAddOwner(); startAddOwner();
}); });
function updateAddOwnerStyles() {
const numOwnerRows = document.querySelectorAll('.owner_row').length;
addOwnerInput.disabled = numOwnerRows >= MAX_OWNERS;
}
updateAddOwnerStyles();
function startAddOwner() { function startAddOwner() {
if (ownerQueryState == OWNER_QUERY_STATE_QUERYING) { if (ownerQueryState == OWNER_QUERY_STATE_QUERYING) {
return; return;
@ -310,6 +318,7 @@
querying = (ownerQueryState == OWNER_QUERY_STATE_QUERYING); querying = (ownerQueryState == OWNER_QUERY_STATE_QUERYING);
addOwnerInput.disabled = querying; addOwnerInput.disabled = querying;
addOwnerButton.disabled = querying; addOwnerButton.disabled = querying;
updateAddOwnerStyles();
} }
function addOwner(username) { function addOwner(username) {
@ -317,12 +326,14 @@
ownerEl.input.value = username; ownerEl.input.value = username;
ownerEl.name.textContent = username; ownerEl.name.textContent = username;
ownerList.appendChild(ownerEl.root); ownerList.appendChild(ownerEl.root);
updateAddOwnerStyles();
} }
ownerList.addEventListener("click", function(ev) { ownerList.addEventListener("click", function(ev) {
if (ev.target.classList.contains("remove_owner")) { if (ev.target.classList.contains("remove_owner")) {
ev.target.parentElement.remove(); ev.target.parentElement.remove();
} }
updateAddOwnerStyles();
}); });
///////////////////// /////////////////////

View File

@ -27,6 +27,7 @@ import (
) )
const maxPersonalProjects = 5 const maxPersonalProjects = 5
const maxProjectOwners = 5
type ProjectTemplateData struct { type ProjectTemplateData struct {
templates.BaseData templates.BaseData
@ -383,6 +384,7 @@ type ProjectEditData struct {
Editing bool Editing bool
ProjectSettings templates.ProjectSettings ProjectSettings templates.ProjectSettings
MaxOwners int
APICheckUsernameUrl string APICheckUsernameUrl string
LogoMaxFileSize int LogoMaxFileSize int
@ -408,6 +410,7 @@ func ProjectNew(c *RequestContext) ResponseData {
BaseData: getBaseDataAutocrumb(c, "New Project"), BaseData: getBaseDataAutocrumb(c, "New Project"),
Editing: false, Editing: false,
ProjectSettings: project, ProjectSettings: project,
MaxOwners: maxProjectOwners,
APICheckUsernameUrl: hmnurl.BuildAPICheckUsername(), APICheckUsernameUrl: hmnurl.BuildAPICheckUsername(),
LogoMaxFileSize: ProjectLogoMaxFileSize, LogoMaxFileSize: ProjectLogoMaxFileSize,
@ -508,6 +511,7 @@ func ProjectEdit(c *RequestContext) ResponseData {
BaseData: getBaseDataAutocrumb(c, "Edit Project"), BaseData: getBaseDataAutocrumb(c, "Edit Project"),
Editing: true, Editing: true,
ProjectSettings: projectSettings, ProjectSettings: projectSettings,
MaxOwners: maxProjectOwners,
APICheckUsernameUrl: hmnurl.BuildAPICheckUsername(), APICheckUsernameUrl: hmnurl.BuildAPICheckUsername(),
LogoMaxFileSize: ProjectLogoMaxFileSize, LogoMaxFileSize: ProjectLogoMaxFileSize,
@ -629,6 +633,10 @@ func ParseProjectEditForm(c *RequestContext) ProjectEditFormResult {
} }
owners := c.Req.Form["owners"] owners := c.Req.Form["owners"]
if len(owners) > maxProjectOwners {
res.RejectionReason = fmt.Sprintf("Projects can have at most %d owners", maxProjectOwners)
return res
}
slug := strings.TrimSpace(c.Req.Form.Get("slug")) slug := strings.TrimSpace(c.Req.Form.Get("slug"))
officialStr := c.Req.Form.Get("official") officialStr := c.Req.Form.Get("official")