💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 8ec36c1b476099591aa3c7c2a0… captured on 2023-04-19 at 23:33:49. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-03-20)
-=-=-=-=-=-=-
0 package gmi
1
2 import (
3 "gemigit/db"
4 "github.com/pitr/gig"
5 )
6
7 func privilegeUpdate(privilege int, first bool) int {
8 if first {
9 return (privilege + 1)%3
10 }
11 if privilege == 0 {
12 return 2
13 }
14 return privilege - 1
15 }
16
17 func privilegeToString(privilege int) string {
18 switch (privilege) {
19 case 0:
20 return "none"
21 case 1:
22 return "read"
23 case 2:
24 return "read and write"
25 }
26 return "Invalid value"
27 }
28
29 func accessFirstOption(privilege int) string {
30 switch (privilege) {
31 case 0:
32 return "Grant read access"
33 case 1:
34 return "Grant write access"
35 default:
36 return "Revoke read and write access"
37 }
38 }
39
40 func accessSecondOption(privilege int) string {
41 switch (privilege) {
42 case 0:
43 return "Grant read and write access"
44 case 1:
45 return "Revoke read access"
46 default:
47 return "Revoke write access"
48 }
49 }
50
51 func changeGroupAccess(user db.User, repo string,
52 name string, first bool) error {
53 r, err := user.GetRepo(repo)
54 if err != nil {
55 return err
56 }
57 groupID, err := db.GetGroupID(name)
58 if err != nil {
59 return err
60 }
61 privilege, err := db.GetGroupAccess(r.RepoID, groupID)
62 if err != nil {
63 return err
64 }
65 privilege = privilegeUpdate(privilege, first)
66 err = db.SetGroupAccess(r.RepoID, groupID, privilege)
67 return err
68 }
69
70 func groupAccessOption(c gig.Context, first bool) error {
71 user, exist := db.GetUser(c.CertHash())
72 if !exist {
73 return c.NoContent(gig.StatusBadRequest, "Invalid group")
74 }
75 err := changeGroupAccess(user, c.Param("repo"),
76 c.Param("group"), first)
77 if err != nil {
78 return c.NoContent(gig.StatusBadRequest, err.Error())
79 }
80 return c.NoContent(gig.StatusRedirectTemporary,
81 "/account/repo/" + c.Param("repo") + "/access")
82 }
83
84 func GroupAccessFirstOption(c gig.Context) error {
85 return groupAccessOption(c, true)
86 }
87
88 func GroupAccessSecondOption(c gig.Context) error {
89 return groupAccessOption(c, false)
90 }
91
92 func changeUserAccess(user db.User, repo string,
93 name string, first bool) error {
94 r, err := user.GetRepo(repo)
95 if err != nil {
96 return err
97 }
98 userID, err := db.GetUserID(name)
99 if err != nil {
100 return err
101 }
102 privilege, err := db.GetUserAccess(r.RepoID, userID)
103 if err != nil {
104 return err
105 }
106 privilege = privilegeUpdate(privilege, first)
107 err = db.SetUserAccess(r.RepoID, userID, privilege)
108 return err
109 }
110
111 func userAccessOption(c gig.Context, first bool) error {
112 user, exist := db.GetUser(c.CertHash())
113 if !exist {
114 return c.NoContent(gig.StatusBadRequest, "Invalid username")
115 }
116 err := changeUserAccess(user, c.Param("repo"), c.Param("user"), first)
117 if err != nil {
118 return c.NoContent(gig.StatusBadRequest, err.Error())
119 }
120 return c.NoContent(gig.StatusRedirectTemporary,
121 "/account/repo/" + c.Param("repo") + "/access")
122 }
123
124 func UserAccessFirstOption(c gig.Context) error {
125 return userAccessOption(c, true)
126 }
127
128 func UserAccessSecondOption(c gig.Context) error {
129 return userAccessOption(c, false)
130 }
131
132 func addAcess(c gig.Context, param string) (string, db.User, db.Repo, error) {
133 query, err := c.QueryString()
134 if err != nil {
135 return "", db.User{}, db.Repo{},
136 c.NoContent(gig.StatusBadRequest, err.Error())
137 }
138 if query == "" {
139 return "", db.User{}, db.Repo{},
140 c.NoContent(gig.StatusInput, param)
141 }
142 user, exist := db.GetUser(c.CertHash())
143 if !exist {
144 return query, user, db.Repo{},
145 c.NoContent(gig.StatusBadRequest, "Invalid entry")
146 }
147
148 repo, err := user.GetRepo(c.Param("repo"))
149 if err != nil {
150 return query, user, db.Repo{},
151 c.NoContent(gig.StatusBadRequest,
152 "Repository not found")
153 }
154 return query, user, repo, nil
155 }
156
157 func AddUserAccess(c gig.Context) error {
158 query, user, repo, err := addAcess(c, "User")
159 if err != nil {
160 return err
161 }
162 err = db.AddUserAccess(user.ID, repo.RepoID, query)
163 if err != nil {
164 return c.NoContent(gig.StatusBadRequest,
165 "Invalid user")
166 }
167 return c.NoContent(gig.StatusRedirectTemporary,
168 "/account/repo/" + repo.Name + "/access")
169 }
170
171 func AddGroupAccess(c gig.Context) error {
172 query, _, repo, err := addAcess(c, "Group")
173 if err != nil {
174 return err
175 }
176 err = db.AddGroupAccess(repo.RepoID, query)
177 if err != nil {
178 return c.NoContent(gig.StatusBadRequest,
179 "Invalid user")
180 }
181 return c.NoContent(gig.StatusRedirectTemporary,
182 "/account/repo/" + repo.Name + "/access")
183 }
184
185 func RemoveUserAccess(c gig.Context) error {
186 user, exist := db.GetUser(c.CertHash())
187 if !exist {
188 return c.NoContent(gig.StatusBadRequest, "Invalid username")
189 }
190 userID, err := db.GetUserID(c.Param("user"))
191 if err != nil {
192 return c.NoContent(gig.StatusBadRequest,
193 "User not found")
194 }
195 repo, err := user.GetRepo(c.Param("repo"))
196 err = db.RemoveUserAccess(repo.RepoID, userID)
197 if err != nil {
198 return c.NoContent(gig.StatusBadRequest,
199 "User doesn't have access")
200 }
201 return c.NoContent(gig.StatusRedirectTemporary,
202 "/account/repo/" + repo.Name + "/access")
203 }
204
205 func RemoveGroupAccess(c gig.Context) error {
206 user, exist := db.GetUser(c.CertHash())
207 if !exist {
208 return c.NoContent(gig.StatusBadRequest, "Invalid username")
209 }
210 groupID, err := db.GetGroupID(c.Param("group"))
211 if err != nil {
212 return c.NoContent(gig.StatusBadRequest,
213 "Group not found")
214 }
215 repo, err := user.GetRepo(c.Param("repo"))
216 err = db.RemoveGroupAccess(repo.RepoID, groupID)
217 if err != nil {
218 return c.NoContent(gig.StatusBadRequest,
219 "Group doesn't have access")
220 }
221 return c.NoContent(gig.StatusRedirectTemporary,
222 "/account/repo/" + repo.Name + "/access")
223 }
224