1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/cli/ops/permissions.rs
Bartek Iwańczuk 4e1abb4f3a
refactor: use OpError instead of ErrBox for errors in ops (#4058)
To better reflect changes in error types in JS from #3662 this PR changes 
default error type used in ops from "ErrBox" to "OpError".

"OpError" is a type that can be sent over to JSON; it has all 
information needed to construct error in JavaScript. That
made "GetErrorKind" trait useless and so it was removed altogether.

To provide compatibility with previous use of "ErrBox" an implementation of
"From<ErrBox> for OpError" was added, however, it is an escape hatch and
ops implementors should strive to use "OpError" directly.
2020-02-23 14:51:29 -05:00

107 lines
3.4 KiB
Rust

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::fs as deno_fs;
use crate::op_error::OpError;
use crate::ops::json_op;
use crate::state::State;
use deno_core::*;
use std::path::Path;
pub fn init(i: &mut Isolate, s: &State) {
i.register_op(
"query_permission",
s.core_op(json_op(s.stateful_op(op_query_permission))),
);
i.register_op(
"revoke_permission",
s.core_op(json_op(s.stateful_op(op_revoke_permission))),
);
i.register_op(
"request_permission",
s.core_op(json_op(s.stateful_op(op_request_permission))),
);
}
#[derive(Deserialize)]
struct PermissionArgs {
name: String,
url: Option<String>,
path: Option<String>,
}
fn resolve_path(path: &str) -> String {
deno_fs::resolve_from_cwd(Path::new(path))
.unwrap()
.to_str()
.unwrap()
.to_string()
}
pub fn op_query_permission(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
let args: PermissionArgs = serde_json::from_value(args)?;
let state = state.borrow();
let resolved_path = args.path.as_ref().map(String::as_str).map(resolve_path);
let perm = state.permissions.get_permission_state(
&args.name,
&args.url.as_ref().map(String::as_str),
&resolved_path.as_ref().map(String::as_str).map(Path::new),
)?;
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
}
pub fn op_revoke_permission(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
let args: PermissionArgs = serde_json::from_value(args)?;
let mut state = state.borrow_mut();
let permissions = &mut state.permissions;
match args.name.as_ref() {
"run" => permissions.allow_run.revoke(),
"read" => permissions.allow_read.revoke(),
"write" => permissions.allow_write.revoke(),
"net" => permissions.allow_net.revoke(),
"env" => permissions.allow_env.revoke(),
"plugin" => permissions.allow_plugin.revoke(),
"hrtime" => permissions.allow_hrtime.revoke(),
_ => {}
};
let resolved_path = args.path.as_ref().map(String::as_str).map(resolve_path);
let perm = permissions.get_permission_state(
&args.name,
&args.url.as_ref().map(String::as_str),
&resolved_path.as_ref().map(String::as_str).map(Path::new),
)?;
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
}
pub fn op_request_permission(
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
let args: PermissionArgs = serde_json::from_value(args)?;
let mut state = state.borrow_mut();
let permissions = &mut state.permissions;
let resolved_path = args.path.as_ref().map(String::as_str).map(resolve_path);
let perm = match args.name.as_ref() {
"run" => Ok(permissions.request_run()),
"read" => Ok(permissions.request_read(
&resolved_path.as_ref().map(String::as_str).map(Path::new),
)),
"write" => Ok(permissions.request_write(
&resolved_path.as_ref().map(String::as_str).map(Path::new),
)),
"net" => permissions.request_net(&args.url.as_ref().map(String::as_str)),
"env" => Ok(permissions.request_env()),
"plugin" => Ok(permissions.request_plugin()),
"hrtime" => Ok(permissions.request_hrtime()),
n => Err(OpError::other(format!("No such permission name: {}", n))),
}?;
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
}