1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-01 03:54:06 -05:00
denoland-deno/cli/ops/permissions.rs

115 lines
3.5 KiB
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, Value};
use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::ErrBox;
use deno_core::ResourceTable;
use deno_core::ZeroCopyBuf;
use std::path::Path;
use std::rc::Rc;
pub fn init(i: &mut CoreIsolate, s: &Rc<State>) {
let t = &CoreIsolate::state(i).borrow().resource_table.clone();
i.register_op(
2020-02-25 09:14:27 -05:00
"op_query_permission",
s.stateful_json_op_sync(t, op_query_permission),
);
i.register_op(
2020-02-25 09:14:27 -05:00
"op_revoke_permission",
s.stateful_json_op_sync(t, op_revoke_permission),
);
2019-11-11 10:33:29 -05:00
i.register_op(
2020-02-25 09:14:27 -05:00
"op_request_permission",
s.stateful_json_op_sync(t, op_request_permission),
2019-11-11 10:33:29 -05:00
);
}
#[derive(Deserialize)]
struct PermissionArgs {
name: String,
url: Option<String>,
path: Option<String>,
}
pub fn op_query_permission(
state: &State,
_resource_table: &mut ResourceTable,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
let args: PermissionArgs = serde_json::from_value(args)?;
let permissions = state.permissions.borrow();
let path = args.path.as_deref();
2020-08-18 16:29:32 -04:00
let perm = match args.name.as_ref() {
"read" => permissions.query_read(&path.as_deref().map(Path::new)),
"write" => permissions.query_write(&path.as_deref().map(Path::new)),
"net" => permissions.query_net_url(&args.url.as_deref())?,
"env" => permissions.query_env(),
"run" => permissions.query_run(),
"plugin" => permissions.query_plugin(),
"hrtime" => permissions.query_hrtime(),
n => {
return Err(ErrBox::new(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
}
pub fn op_revoke_permission(
state: &State,
_resource_table: &mut ResourceTable,
2019-08-26 08:50:21 -04:00
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
let args: PermissionArgs = serde_json::from_value(args)?;
let mut permissions = state.permissions.borrow_mut();
let path = args.path.as_deref();
2020-08-18 16:29:32 -04:00
let perm = match args.name.as_ref() {
"read" => permissions.revoke_read(&path.as_deref().map(Path::new)),
"write" => permissions.revoke_write(&path.as_deref().map(Path::new)),
"net" => permissions.revoke_net(&args.url.as_deref())?,
"env" => permissions.revoke_env(),
"run" => permissions.revoke_run(),
"plugin" => permissions.revoke_plugin(),
"hrtime" => permissions.revoke_hrtime(),
n => {
return Err(ErrBox::new(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
}
2019-11-11 10:33:29 -05:00
pub fn op_request_permission(
state: &State,
_resource_table: &mut ResourceTable,
2019-11-11 10:33:29 -05:00
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
2019-11-11 10:33:29 -05:00
let args: PermissionArgs = serde_json::from_value(args)?;
let permissions = &mut state.permissions.borrow_mut();
let path = args.path.as_deref();
2019-11-11 10:33:29 -05:00
let perm = match args.name.as_ref() {
2020-08-18 16:29:32 -04:00
"read" => permissions.request_read(&path.as_deref().map(Path::new)),
"write" => permissions.request_write(&path.as_deref().map(Path::new)),
"net" => permissions.request_net(&args.url.as_deref())?,
"env" => permissions.request_env(),
"run" => permissions.request_run(),
"plugin" => permissions.request_plugin(),
"hrtime" => permissions.request_hrtime(),
n => {
return Err(ErrBox::new(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
2019-11-11 10:33:29 -05:00
}