2021-07-23 03:09:46 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.handmade.network/hmn/hmn/src/db"
|
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
|
|
|
"git.handmade.network/hmn/hmn/src/oops"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CanEditProject(c *RequestContext, user *models.User, projectId int) (bool, error) {
|
|
|
|
if user != nil {
|
|
|
|
if user.IsStaff {
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
owners, err := FetchProjectOwners(c, projectId)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, owner := range owners {
|
|
|
|
if owner.ID == user.ID {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FetchProjectOwners(c *RequestContext, projectId int) ([]*models.User, error) {
|
|
|
|
var result []*models.User
|
|
|
|
c.Perf.StartBlock("SQL", "Fetching project owners")
|
|
|
|
type ownerQuery struct {
|
|
|
|
Owner models.User `db:"auth_user"`
|
|
|
|
}
|
|
|
|
ownerQueryResult, err := db.Query(c.Context(), c.Conn, ownerQuery{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM
|
|
|
|
auth_user
|
2021-08-03 03:27:59 +00:00
|
|
|
INNER JOIN handmade_user_projects AS uproj ON uproj.user_id = auth_user.id
|
2021-07-23 03:09:46 +00:00
|
|
|
WHERE
|
2021-08-03 03:27:59 +00:00
|
|
|
uproj.project_id = $1
|
2021-07-23 03:09:46 +00:00
|
|
|
`,
|
|
|
|
projectId,
|
|
|
|
)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
if err != nil {
|
|
|
|
return result, oops.New(err, "failed to fetch owners for project")
|
|
|
|
}
|
|
|
|
for _, ownerRow := range ownerQueryResult.ToSlice() {
|
|
|
|
result = append(result, &ownerRow.(*ownerQuery).Owner)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|