/
edit_sharing.go
137 lines (117 loc) · 3.25 KB
/
edit_sharing.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
package randomization
import (
"net/http"
"strings"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/user"
)
// editSharing
func editSharing(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
pkey := r.FormValue("pkey")
shr := strings.Split(pkey, "::")
owner := shr[0]
projectName := shr[1]
if strings.ToLower(owner) != strings.ToLower(user.String()) {
msg := "Only the owner of a project can manage sharing."
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
return
}
sharedUsers, err := getSharedUsers(ctx, pkey)
if err != nil {
sharedUsers = make([]string, 0)
log.Infof(ctx, "editSharing failed to retrieve sharing: %v %v", projectName, owner)
}
tvals := struct {
User string
LoggedIn bool
SharedUsers []string
AnySharedUsers bool
ProjectName string
Pkey string
}{
User: user.String(),
LoggedIn: user != nil,
SharedUsers: sharedUsers,
AnySharedUsers: len(sharedUsers) > 0,
ProjectName: projectName,
Pkey: pkey,
}
if err := tmpl.ExecuteTemplate(w, "edit_sharing.html", tvals); err != nil {
log.Errorf(ctx, "editSharing failed to execute template: %v", err)
}
}
// editSharingConfirm
func editSharingConfirm(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Serve404(w)
return
}
ctx := appengine.NewContext(r)
user := user.Current(ctx)
pkey := r.FormValue("pkey")
spkey := strings.Split(pkey, "::")
projectName := spkey[1]
ap := r.FormValue("additional_people")
addUsers := cleanSplit(ap, ",")
for k, x := range addUsers {
addUsers[k] = strings.ToLower(x)
}
// Gmail addresses don't use @gmail.com.
invalidEmails := make([]string, 0)
for k, x := range addUsers {
uparts := strings.Split(x, "@")
if len(uparts) != 2 {
invalidEmails = append(invalidEmails, x)
} else {
if uparts[1] == "gmail.com" {
addUsers[k] = uparts[0]
}
}
}
if len(invalidEmails) > 0 {
msg := "The project was not shared because the following email addresses are not valid: "
msg += strings.Join(invalidEmails, ", ") + "."
rmsg := "Return to project"
messagePage(w, r, user, msg, rmsg, "/project_dashboard?pkey="+pkey)
return
}
err := addSharing(ctx, pkey, addUsers)
if err != nil {
msg := "Datastore error: unable to update sharing information."
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
log.Errorf(ctx, "editSharingConfirm [1]: %v", err)
return
}
removeUsers := r.Form["remove_users"]
err = removeSharing(ctx, pkey, removeUsers)
if err != nil {
msg := "Datastore error: unable to update sharing information."
rmsg := "Return to dashboard"
messagePage(w, r, user, msg, rmsg, "/dashboard")
log.Errorf(ctx, "editSharingConfirm [2]: %v", err)
return
}
tvals := struct {
User string
LoggedIn bool
ProjectName string
Pkey string
}{
User: user.String(),
LoggedIn: user != nil,
ProjectName: projectName,
Pkey: pkey,
}
if err := tmpl.ExecuteTemplate(w, "edit_sharing_confirm.html", tvals); err != nil {
log.Errorf(ctx, "Failed to execute template: %v", err)
}
}