2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op(
|
|
|
|
"permissions",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_permissions))),
|
|
|
|
);
|
|
|
|
i.register_op(
|
|
|
|
"revoke_permission",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_revoke_permission))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
pub fn op_permissions(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"run": state.permissions.allows_run(),
|
|
|
|
"read": state.permissions.allows_read(),
|
|
|
|
"write": state.permissions.allows_write(),
|
|
|
|
"net": state.permissions.allows_net(),
|
|
|
|
"env": state.permissions.allows_env(),
|
|
|
|
"hrtime": state.permissions.allows_hrtime(),
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct RevokePermissionArgs {
|
|
|
|
permission: String,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_revoke_permission(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: RevokePermissionArgs = serde_json::from_value(args)?;
|
|
|
|
let permission = args.permission.as_ref();
|
2019-08-14 11:03:02 -04:00
|
|
|
match permission {
|
|
|
|
"run" => state.permissions.revoke_run(),
|
|
|
|
"read" => state.permissions.revoke_read(),
|
|
|
|
"write" => state.permissions.revoke_write(),
|
|
|
|
"net" => state.permissions.revoke_net(),
|
|
|
|
"env" => state.permissions.revoke_env(),
|
|
|
|
"hrtime" => state.permissions.revoke_hrtime(),
|
|
|
|
_ => Ok(()),
|
|
|
|
}?;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|