Skip to content

Commit ac6ab0e

Browse files
committed
Add util for self-verification of subgroups
1 parent 1d991e8 commit ac6ab0e

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

resources/queries/queryfile.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ SELECT *
6464
FROM group_verifications
6565
WHERE group_name = :group_name;
6666

67+
--name: find-group-verifications-for-users-groups
68+
SELECT *
69+
FROM group_verifications gv
70+
JOIN (
71+
SELECT name
72+
FROM groups
73+
WHERE (
74+
"user" = :username
75+
AND
76+
inactive IS NOT true
77+
)
78+
) g
79+
ON (
80+
gv.group_name = g.name
81+
)
82+
6783
--name: verify-group!
6884
INSERT INTO group_verifications (group_name, verified_by)
6985
VALUES (:group_name, :verifying_username);

src/clojars/db.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@
177177
{:connection db
178178
:result-set-fn first}))
179179

180+
(defn find-group-verifications-for-users-groups [db username]
181+
(sql/find-group-verifications-for-users-groups {:username username}
182+
{:connection db}))
183+
180184
(defn verify-group! [db username group-name]
181185
(when-not (find-group-verification db group-name)
182186
(sql/verify-group! {:group_name group-name

src/clojars/verification.clj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,26 @@
113113

114114
:else
115115
(err request "You are not an active member of the group."))))))
116+
117+
(defn verify-group-by-parent-group
118+
"Verifies a group that is a subgroup of an already verified group."
119+
[db {:as request :keys [username group]}]
120+
(let [group (str/lower-case group)
121+
group-verification (db/find-group-verification db group)]
122+
(cond
123+
(not (valid-identifier? group))
124+
(err request "The group name is not a valid reverse-domain name.")
125+
126+
group-verification
127+
(err request (format "Group already verified by user '%s' on %s."
128+
(:verified_by group-verification)
129+
(common/format-date (:created group-verification))))
130+
131+
:else
132+
(if-some [parent-group-name (some
133+
(fn [{:keys [group_name]}]
134+
(when (str/starts-with? group (str group_name "."))
135+
group_name))
136+
(db/find-group-verifications-for-users-groups db username))]
137+
(verify-group db (assoc request :parent-group parent-group-name) username group)
138+
(err request "The group is not a subgroup of a verified group.")))))

test/clojars/unit/verification_test.clj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,39 @@
134134
:domain "foo.com"}))))
135135
(is (db/group-activenames help/*db* "com.foo"))
136136
(is (db/find-group-verification help/*db* "com.foo"))))
137+
138+
(deftest verify-group-by-parent-group-with-invalid-group
139+
(are [given] (= "The group name is not a valid reverse-domain name."
140+
(:error (nut/verify-group-by-parent-group help/*db* {:group given})))
141+
"bar"
142+
"com.bar;rm -rf /"
143+
".com"
144+
"..com"))
145+
146+
(deftest verify-group-by-parent-group-with-already-verified-group
147+
(db/verify-group! help/*db* "dantheman" "com.foo.bar")
148+
(is (match?
149+
{:error (format "Group already verified by user 'dantheman' on %s."
150+
(common/format-date (Date.)))}
151+
(nut/verify-group-by-parent-group help/*db* {:group "com.foo.bar"}))))
152+
153+
(deftest verify-group-by-parent-group-with-non-subgroup
154+
(db/add-group help/*db* "dantheman" "com.foo")
155+
(db/verify-group! help/*db* "dantheman" "com.foo")
156+
(is (match?
157+
{:error "The group is not a subgroup of a verified group."}
158+
(nut/verify-group-by-parent-group help/*db* {:group "com.bar"
159+
:username "dantheman"})))
160+
(is (match?
161+
{:error "The group is not a subgroup of a verified group."}
162+
(nut/verify-group-by-parent-group help/*db* {:group "com.food"
163+
:username "dantheman"}))))
164+
165+
(deftest verify-group-by-parent-group-that-is-subgroup
166+
(db/add-group help/*db* "dantheman" "com.foo")
167+
(db/verify-group! help/*db* "dantheman" "com.foo")
168+
(is (match?
169+
{:message "The group 'com.foo.bar' has been verified."
170+
:parent-group "com.foo"}
171+
(nut/verify-group-by-parent-group help/*db* {:group "com.foo.bar"
172+
:username "dantheman"}))))

0 commit comments

Comments
 (0)