Skip to content

Commit b80be6d

Browse files
committed
Make the repo and users keys in ACL
1 parent a4d3dcd commit b80be6d

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

src/lib.rs

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ extern crate pest_derive;
3535
#[macro_use]
3636
extern crate serde_json;
3737

38+
use std::collections::HashMap;
39+
use tracing;
40+
3841
pub mod cache;
3942
pub mod filter;
4043
pub mod graphql;
@@ -299,20 +302,10 @@ pub fn normalize_path(path: &std::path::Path) -> std::path::PathBuf {
299302
ret
300303
}
301304

302-
#[derive(Debug, serde::Deserialize)]
303-
struct Acl {
304-
pub repo: Vec<Repo>,
305-
}
306-
307-
#[derive(Debug, serde::Deserialize)]
308-
struct Repo {
309-
pub name: String,
310-
pub user: Vec<User>,
311-
}
305+
type Acl = HashMap<String, HashMap<String, User>>;
312306

313307
#[derive(Debug, serde::Deserialize)]
314308
struct User {
315-
pub name: String,
316309
pub whitelist: Option<String>,
317310
pub blacklist: Option<String>,
318311
}
@@ -321,44 +314,30 @@ pub fn get_whitelist(acl: &str, user: &str, repo: &str) -> JoshResult<filter::Fi
321314
let acl = std::fs::read_to_string(acl).map_err(|_| josh_error("failed to read acl file"))?;
322315
let acl: Acl = toml::from_str(&acl)
323316
.map_err(|err| josh_error(format!("failed to parse acl file: {}", err).as_str()))?;
324-
for r in acl.repo {
325-
if r.name == repo {
326-
for u in r.user {
327-
if u.name == user {
328-
match u.whitelist {
329-
Some(w) => {
330-
let filter = filter::parse(&w)?;
331-
return Ok(filter);
332-
}
333-
_ => return Ok(filter::empty()),
334-
}
335-
}
336-
}
337-
}
338-
}
339-
340-
return Ok(filter::empty());
317+
return Ok(match acl.get(repo) {
318+
Some(r) => match r.get(user) {
319+
Some(u) => match &u.whitelist {
320+
Some(w) => filter::parse(&w)?,
321+
_ => filter::nop(),
322+
},
323+
_ => filter::empty(),
324+
},
325+
_ => filter::empty(),
326+
});
341327
}
342328

343329
pub fn get_blacklist(acl: &str, user: &str, repo: &str) -> JoshResult<filter::Filter> {
344330
let acl = std::fs::read_to_string(acl).map_err(|_| josh_error("failed to read acl file"))?;
345331
let acl: Acl = toml::from_str(&acl)
346332
.map_err(|err| josh_error(format!("failed to parse acl file: {}", err).as_str()))?;
347-
for r in acl.repo {
348-
if r.name == repo {
349-
for u in r.user {
350-
if u.name == user {
351-
match u.blacklist {
352-
Some(b) => {
353-
let filter = filter::parse(&b)?;
354-
return Ok(filter);
355-
}
356-
_ => return Ok(filter::nop()),
357-
}
358-
}
359-
}
360-
}
361-
}
362-
363-
return Ok(filter::nop());
333+
return Ok(match acl.get(repo) {
334+
Some(r) => match r.get(user) {
335+
Some(u) => match &u.blacklist {
336+
Some(b) => filter::parse(&b)?,
337+
_ => filter::empty(),
338+
},
339+
_ => filter::nop(),
340+
},
341+
_ => filter::nop(),
342+
});
364343
}

tests/filter/permissions.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,13 @@
404404

405405
# acl
406406
$ cat << EOF > acl.toml
407-
> [[repo]]
408-
> name = "test"
409-
> [[repo.user]]
410-
> name = "LMG"
407+
> [test]
408+
> [test.LMG]
411409
> whitelist = ":/"
412410
> blacklist = ":empty"
413411
>
414412
> EOF
413+
# doesn't work
415414
$ josh-filter -s :/ master --check-permission -a acl.toml -u bob -r test --update refs/josh/filtered
416415
Warning: reference refs/josh/filtered wasn't updated
417416
[1] :[
@@ -429,6 +428,7 @@
429428
[4] :INVERT
430429
[13] _invert
431430
[16] _paths
431+
# works
432432
$ josh-filter -s :/ master --check-permission -a acl.toml -u LMG -r test --update refs/josh/filtered
433433
[1] :[
434434
:/b

0 commit comments

Comments
 (0)