-
Notifications
You must be signed in to change notification settings - Fork 1
/
deletion_requests_controller.go
161 lines (149 loc) · 5.21 KB
/
deletion_requests_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package webui
import (
"fmt"
"net/http"
"github.com/APTrust/registry/common"
"github.com/APTrust/registry/forms"
"github.com/APTrust/registry/pgmodels"
"github.com/gin-gonic/gin"
)
// DeletionRequestShow shows the deletion request with the specified id.
//
// Note that this shows a read-only view of the request. It does not include
// the Approve/Cancel buttons. This read-only view may be available to users
// who do not have permission to initiate, approve, or cancel deletion requests
// but who still need a read-only view of the requests that have been submitted.
//
// Deletions apply to files and/or intellectual objects. The methods for
// initiating, approving and rejecting deletion requests are in the
// Generic Files Controller (for files) and the Intellectual Objects Controller
// (for objects).
//
// GET /deletions/show/:id
func DeletionRequestShow(c *gin.Context) {
req := NewRequest(c)
err := deletionRequestLoad(req)
if AbortIfError(c, err) {
return
}
c.HTML(http.StatusOK, "deletions/show.html", req.TemplateData)
}
// DeletionRequestIndex shows list of deletion requests.
// GET /deletions
func DeletionRequestIndex(c *gin.Context) {
req := NewRequest(c)
var deletions []*pgmodels.DeletionRequestView
err := req.LoadResourceList(&deletions, "requested_at", "desc", forms.NewDeletionRequestFilterForm)
if AbortIfError(c, err) {
return
}
c.HTML(http.StatusOK, "deletions/index.html", req.TemplateData)
}
func deletionRequestLoad(req *Request) error {
deletionRequest, err := pgmodels.DeletionRequestByID(req.Auth.ResourceID)
if err != nil {
return err
}
req.TemplateData["deletionRequest"] = deletionRequest
if deletionRequest.WorkItemID > 0 {
req.TemplateData["workItemURL"] = fmt.Sprintf("%s/work_items/show/%d",
req.BaseURL(),
deletionRequest.WorkItemID)
}
return nil
}
// DeletionRequestReview displays a page on which an institutional
// admin can review a requested file deletion and choose whether to approve
// or cancel it.
//
// GET /deletions/review/:id?token=<token>
func DeletionRequestReview(c *gin.Context) {
req := NewRequest(c)
del, err := NewDeletionForReview(req.Auth.ResourceID, req.CurrentUser, req.BaseURL(), c.Query("token"))
if AbortIfError(c, err) {
return
}
// Present the page describing the request, and if it hasn't
// already been cancelled or approved, give the user the option
// to cancel or approve.
req.TemplateData["deletionRequest"] = del.DeletionRequest
req.TemplateData["token"] = c.Query("token")
if len(del.DeletionRequest.IntellectualObjects) > 0 {
req.TemplateData["itemType"] = "object"
req.TemplateData["itemIdentifier"] = del.DeletionRequest.IntellectualObjects[0].Identifier
req.TemplateData["object"] = del.DeletionRequest.IntellectualObjects[0]
} else if len(del.DeletionRequest.GenericFiles) > 0 {
req.TemplateData["itemType"] = "file"
req.TemplateData["itemIdentifier"] = del.DeletionRequest.GenericFiles[0].Identifier
req.TemplateData["file"] = del.DeletionRequest.GenericFiles[0]
} else {
common.Context().Log.Info().Msgf("DeletionRequest with ID %d has no associated files or objects.", req.Auth.ResourceID)
AbortIfError(c, common.ErrInternal)
return
}
template := "deletions/review.html"
if del.DeletionRequest.ConfirmedByID > 0 {
template = "deletions/already_approved.html"
} else if del.DeletionRequest.CancelledByID > 0 {
template = "deletions/already_cancelled.html"
}
c.HTML(http.StatusOK, template, req.TemplateData)
}
// DeletionRequestApprove handles the case where an institutional
// admin approves a deletion request. Note that the token comes
// in through the post form here, not through the URL.
//
// POST /deletions/approve/:id
func DeletionRequestApprove(c *gin.Context) {
req := NewRequest(c)
del, err := NewDeletionForReview(req.Auth.ResourceID, req.CurrentUser, req.BaseURL(), c.PostForm("token"))
if AbortIfError(c, err) {
return
}
del.DeletionRequest.Confirm(req.CurrentUser)
err = del.DeletionRequest.Save()
if AbortIfError(c, err) {
return
}
_, err = del.CreateAndQueueWorkItem()
if AbortIfError(c, err) {
return
}
_, err = del.CreateApprovalAlert()
if AbortIfError(c, err) {
return
}
req.TemplateData["deletionRequest"] = del.DeletionRequest
template := "deletions/approved_file.html"
if len(del.DeletionRequest.IntellectualObjects) > 0 {
template = "deletions/approved_object.html"
}
c.HTML(http.StatusOK, template, req.TemplateData)
}
// DeletionRequestCancel handles the case where an institutional
// admin cancels (rejects) a file deletion request. Token comes in
// through post form, not query string.
//
// POST /deletions/cancel/:id
func DeletionRequestCancel(c *gin.Context) {
req := NewRequest(c)
del, err := NewDeletionForReview(req.Auth.ResourceID, req.CurrentUser, req.BaseURL(), c.PostForm("token"))
if AbortIfError(c, err) {
return
}
del.DeletionRequest.Cancel(req.CurrentUser)
err = del.DeletionRequest.Save()
if AbortIfError(c, err) {
return
}
_, err = del.CreateCancellationAlert()
if AbortIfError(c, err) {
return
}
req.TemplateData["deletionRequest"] = del.DeletionRequest
template := "deletions/cancelled_file.html"
if len(del.DeletionRequest.IntellectualObjects) > 0 {
template = "deletions/cancelled_object.html"
}
c.HTML(http.StatusOK, template, req.TemplateData)
}