Skip to content

Commit 891147e

Browse files
authored
list-permissions command (#34)
1 parent a935e6a commit 891147e

File tree

9 files changed

+231
-62
lines changed

9 files changed

+231
-62
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ Command line interface to manipulate ProtonLights projects.
99
- `new-sequence <admin-key> <name> <music-file>`: Init a sequence
1010
- `remove-sequence <admin-key> <name>`: Removes a sequence and deletes its files
1111
- `id-user <private-key>`: Identify user by ssh key (public key in repo)
12-
- `list-permissions`: Get list of all available permissions
12+
- `list-permissions <private-key>`: Get list of user's permissions
1313
- `set-permission <admin-key> (add | remove) <name> <permission> [<target>]`: Change user permissions
14-
- `list-editable [TODO]`: Get list of editable files for a given user
1514
- `resection-sequence [TODO]`: (Re-)Section a sequence
1615
- On init, section as section1.
1716
- Number each section, and don't delete.

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate docopt;
55

66
use std::env;
77
use std::path::Path;
8+
use rustc_serialize::json;
89
use docopt::Docopt;
910

1011
use proton_cli::error::Error;
@@ -22,7 +23,7 @@ Usage:
2223
./proton new-sequence <admin-key> <name> <music-file>
2324
./proton remove-sequence <admin-key> <name>
2425
./proton id-user <private-key>
25-
./proton list-permissions
26+
./proton list-permissions <private-key>
2627
./proton set-permission <admin-key> (add | remove) <name> <permission> [<target>]
2728
./proton (-h | --help)
2829
@@ -118,10 +119,10 @@ fn run_remove_sequence(args: Args) -> Result<(), Error> {
118119
proton_cli::remove_sequence(&admin_key_path, &name)
119120
}
120121

121-
#[allow(unused_variables)]
122122
fn run_list_permissions(args: Args) -> Result<(), Error> {
123-
let permissions = proton_cli::get_permissions();
124-
Ok(println!("{}", permissions.join("\n")))
123+
let private_key = args.arg_private_key;
124+
proton_cli::get_permissions(&private_key.unwrap())
125+
.map(|p| println!("{}", json::as_pretty_json(&p)))
125126
}
126127

127128
fn run_set_permission(args: Args) -> Result<(), Error> {

src/permissions.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ use git2::Signature;
66
use error::Error;
77
use project_types::{User, Permission, PermissionEnum};
88
use utils;
9+
use user;
910

1011

11-
pub fn get_permissions() -> Vec<&'static str> {
12-
vec![
13-
"Administrate",
14-
"EditSeq",
15-
"EditSeqSec"
16-
]
17-
}
18-
1912
pub fn set_permission(
2013
auth_user: &User,
2114
add: bool,
@@ -56,6 +49,9 @@ pub fn set_permission(
5649
auth_user.name, change_type, perm, target_username);
5750

5851
utils::commit_all(None::<&Path>, &signature, &msg)
59-
6052
}
6153

54+
pub fn get_permissions<P: AsRef<Path>> (user_key_path: P
55+
) -> Result<Vec<Permission>, Error> {
56+
user::id_user(&user_key_path).map(|user| user.permissions)
57+
}

src/project_types/permissions.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl Permission {
5656
let target_str = target.to_owned().unwrap();
5757
let targets: Vec<&str> = target_str.split(",").collect();
5858
if targets.len() != 2 {
59+
println!("EditSeqSec target must be of the form \"name,section\"");
5960
false
6061
} else {
6162
let seq_name = targets[0];
@@ -67,9 +68,16 @@ impl Permission {
6768
let project = try!(utils::read_protonfile(None::<&Path>));
6869
match project.find_sequence_by_name(&seq_name) {
6970
Some(seq) => {
70-
section_num > 0 && section_num <= seq.num_sections
71+
let in_range = section_num > 0 && section_num <= seq.num_sections;
72+
if !in_range {
73+
println!("EditSeqSec target must be of the form \"name,section\"");
74+
}
75+
in_range
76+
},
77+
None => {
78+
println!("EditSeqSec target must be of the form \"name,section\"");
79+
false
7180
},
72-
None => false,
7381
}
7482

7583
}
@@ -83,5 +91,4 @@ impl Permission {
8391
Err(Error::InvalidPermissionTarget)
8492
}
8593
}
86-
8794
}

src/sequence.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,3 @@ fn get_music_duration_sec<P: AsRef<Path>>(path: P) -> Result<u32, Error> {
181181
let duration = duration_time.as_seconds() as u32;
182182
Ok(duration)
183183
}
184-

src/user.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,3 @@ pub fn id_user<P: AsRef<Path>>(private_key_path: P) -> Result<User, Error> {
108108

109109
Err(Error::UserNotFound)
110110
}
111-
112-
113-

tests/common/setup.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::{Path, PathBuf};
66
use self::tempdir::TempDir;
77

88
use proton_cli;
9+
use proton_cli::project_types::PermissionEnum;
910

1011
use super::rsa_keys::{self, TestKey};
1112

@@ -75,3 +76,29 @@ pub fn try_make_sequence(admin_key_path: &Path, name: &str, music_file: &str) {
7576
seq_dir_path.push(&seq.music_file_name);
7677
assert!(seq_dir_path.exists());
7778
}
79+
80+
/// Tries to modify a user's permission
81+
/// Panics on error
82+
///
83+
/// Impure.
84+
pub fn try_set_permission<P: AsRef<Path>>(
85+
root_path: &Path,
86+
auth_private_key_path: P,
87+
add: bool,
88+
target_username: &str,
89+
permission: PermissionEnum,
90+
target: Option<String>,
91+
) {
92+
let auth_user = proton_cli::id_user(&auth_private_key_path)
93+
.expect("Auth user not found");
94+
95+
proton_cli::set_permission(
96+
&auth_user,
97+
add,
98+
&target_username,
99+
permission.to_owned(),
100+
target.to_owned()
101+
).expect("Error setting permission");
102+
103+
super::assert_repo_no_modified_files(&root_path);
104+
}

tests/get_permissions.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
extern crate proton_cli;
2+
3+
mod common;
4+
5+
use std::path::Path;
6+
7+
use common::setup;
8+
use common::rsa_keys::TestKey;
9+
use proton_cli::project_types::PermissionEnum;
10+
11+
12+
#[test]
13+
fn works_with_valid_key_no_permissions() {
14+
let root = setup::setup_init_cd();
15+
let root_key_path = common::make_key_file(&root.path(), "root.pem", TestKey::RootKeyPem);
16+
let user_key_path = common::make_key_file(&root.path(), "a.pem", TestKey::GoodKeyPem);
17+
let name = "UserA";
18+
19+
setup::try_new_user(
20+
&root_key_path.as_path(),
21+
root.path(),
22+
&name,
23+
"a.pub",
24+
TestKey::GoodKeyPub);
25+
26+
let permissions = proton_cli::get_permissions(&user_key_path)
27+
.expect("Error getting permissions");
28+
29+
assert_eq!(permissions.len(), 0);
30+
}
31+
32+
#[test]
33+
fn works_with_valid_key_one_permission() {
34+
let root = setup::setup_init_cd();
35+
let root_key_path = common::make_key_file(&root.path(), "root.pem", TestKey::RootKeyPem);
36+
let user_key_path = common::make_key_file(&root.path(), "a.pem", TestKey::GoodKeyPem);
37+
let name = "UserA";
38+
39+
setup::try_new_user(
40+
&root_key_path.as_path(),
41+
root.path(),
42+
&name,
43+
"a.pub",
44+
TestKey::GoodKeyPub);
45+
46+
setup::try_make_sequence(
47+
&root_key_path.as_path(),
48+
"asdf",
49+
"Dissonance.ogg"
50+
);
51+
52+
setup::try_set_permission(
53+
&root.path(),
54+
&root_key_path,
55+
true,
56+
&name,
57+
PermissionEnum::EditSeq,
58+
Some("asdf".to_owned()));
59+
60+
let permissions = proton_cli::get_permissions(&user_key_path)
61+
.expect("Error getting permissions");
62+
63+
assert_eq!(permissions.len(), 1);
64+
assert_eq!(&permissions[0].which, &PermissionEnum::EditSeq);
65+
assert!(&permissions[0].target.is_some());
66+
assert_eq!(permissions[0].target, Some("asdf".to_owned()));
67+
}
68+
69+
#[test]
70+
fn works_with_valid_key_all_permissions() {
71+
let root = setup::setup_init_cd();
72+
let root_key_path = common::make_key_file(&root.path(), "root.pem", TestKey::RootKeyPem);
73+
let user_key_path = common::make_key_file(&root.path(), "a.pem", TestKey::GoodKeyPem);
74+
let name = "UserA";
75+
76+
setup::try_new_user(
77+
&root_key_path.as_path(),
78+
root.path(),
79+
&name,
80+
"a.pub",
81+
TestKey::GoodKeyPub);
82+
83+
setup::try_make_sequence(
84+
&root_key_path.as_path(),
85+
"asdf",
86+
"Dissonance.ogg"
87+
);
88+
89+
setup::try_make_sequence(
90+
&root_key_path.as_path(),
91+
"ghjk",
92+
"GlorytotheBells.ogg"
93+
);
94+
95+
setup::try_set_permission(
96+
&root.path(),
97+
&root_key_path,
98+
true,
99+
&name,
100+
PermissionEnum::Administrate,
101+
None::<String>);
102+
103+
setup::try_set_permission(
104+
&root.path(),
105+
&root_key_path,
106+
true,
107+
&name,
108+
PermissionEnum::EditSeq,
109+
Some("asdf".to_owned()));
110+
111+
setup::try_set_permission(
112+
&root.path(),
113+
&root_key_path,
114+
true,
115+
&name,
116+
PermissionEnum::EditSeqSec,
117+
Some("ghjk,1".to_owned()));
118+
119+
let permissions = proton_cli::get_permissions(&user_key_path)
120+
.expect("Error getting permissions");
121+
122+
assert_eq!(permissions.len(), 3);
123+
assert_eq!(&permissions[0].which, &PermissionEnum::Administrate);
124+
assert!(&permissions[0].target.is_none());
125+
assert_eq!(&permissions[1].which, &PermissionEnum::EditSeq);
126+
assert!(&permissions[1].target.is_some());
127+
assert_eq!(permissions[1].target, Some("asdf".to_owned()));
128+
assert_eq!(&permissions[2].which, &PermissionEnum::EditSeqSec);
129+
assert!(&permissions[2].target.is_some());
130+
assert_eq!(permissions[2].target, Some("ghjk,1".to_owned()));
131+
}
132+
133+
#[test]
134+
#[should_panic(expected = "Error getting permissions: Io")]
135+
fn fails_with_invalid_key_path() {
136+
let root = setup::setup_init_cd();
137+
let root_key_path = common::make_key_file(&root.path(), "root.pem", TestKey::RootKeyPem);
138+
let user_key_path = Path::new("invalid");
139+
let name = "UserA";
140+
141+
setup::try_new_user(
142+
&root_key_path.as_path(),
143+
root.path(),
144+
&name,
145+
"a.pub",
146+
TestKey::GoodKeyPub);
147+
148+
let _ = proton_cli::get_permissions(&user_key_path)
149+
.expect("Error getting permissions");
150+
}
151+
152+
#[test]
153+
#[should_panic(expected = "Error getting permissions: Ssl")]
154+
fn fails_with_invalid_key() {
155+
let root = setup::setup_init_cd();
156+
let root_key_path = common::make_key_file(&root.path(), "root.pem", TestKey::RootKeyPem);
157+
let user_key_path = common::make_key_file(&root.path(), "a.pub", TestKey::GoodKeyPub);
158+
let name = "UserA";
159+
160+
setup::try_new_user(
161+
&root_key_path.as_path(),
162+
root.path(),
163+
&name,
164+
"aa.pub",
165+
TestKey::GoodKeyPub);
166+
167+
let _ = proton_cli::get_permissions(&user_key_path)
168+
.expect("Error getting permissions");
169+
}

0 commit comments

Comments
 (0)