Skip to content

Commit

Permalink
dedicated permission
Browse files Browse the repository at this point in the history
  • Loading branch information
MXWXZ committed Jan 13, 2025
1 parent 29eaf39 commit 8d39669
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dev
## Changes
1. Dedicated manage permission.

# v0.5.0
## Changes
1. Update `skynet_api` version.
Expand Down
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ skynet_api = { version = "0.4", features = [
"plugin-api",
"request-param",
"service-skynet",
"viewer",
] }
skynet_macro = "0.3"
sea-orm-migration = { version = "1.1", features = [
Expand Down
8 changes: 6 additions & 2 deletions task/frontend/src/components/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const TaskCard = () => {
tip={intl.get('pages.task.stop.tip')}
color="#ff4d4f"
perm={UserPerm.PermWrite}
permName="manage.plugin"
permName="manage.4adaf7d3-b877-43c3-82bd-da3689dc3920"
onClick={() => handleStop(intl, ref, row.id, row.name)}
disabled={row.result != undefined}
/>,
Expand All @@ -184,7 +184,11 @@ const TaskCard = () => {
danger
onClick={() => handleDeleteAll(intl, ref)}
disabled={
!checkPerm(access, 'manage.notification', UserPerm.PermWrite)
!checkPerm(
access,
'manage.4adaf7d3-b877-43c3-82bd-da3689dc3920',
UserPerm.PermWrite,
)
}
>
<FormattedMessage id="app.op.deleteall" />
Expand Down
4 changes: 1 addition & 3 deletions task/frontend/src/components/output.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TableBtn from '@/common_components/layout/table/tableBtn';
import { getIntl, UserPerm } from '@/utils';
import { getIntl } from '@/utils';
import { ProfileOutlined } from '@ant-design/icons';
import { Modal } from 'antd';
import { useState } from 'react';
Expand All @@ -18,8 +18,6 @@ const TaskOutput: React.FC<TaskOutputProps> = (props) => {
<TableBtn
icon={ProfileOutlined}
tip={intl.get('pages.task.output.tip')}
perm={UserPerm.PermRead}
permName="manage.plugin"
onClick={() => setIsModalOpen(true)}
/>
<Modal
Expand Down
2 changes: 1 addition & 1 deletion task/frontend/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const IndexPage = () => {
return (
<MainLayout
title="titles.task"
access="manage.plugin"
access="manage.4adaf7d3-b877-43c3-82bd-da3689dc3920"
perm={UserPerm.PermRead}
>
<MainContainer
Expand Down
41 changes: 28 additions & 13 deletions task/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::{path::PathBuf, sync::OnceLock};
use std::{
path::PathBuf,
sync::{Arc, OnceLock},
};

use actix_cloud::{
actix_web::web::Data,
i18n::{i18n, Locale},
memorydb,
router::CSRFType,
state::{GlobalState, ServerHandle},
tokio::runtime::Runtime,
Expand All @@ -20,13 +24,15 @@ use skynet_api::{
},
registry::Registry,
},
permission::{IDTypes::PermManagePluginID, PermChecker, PERM_READ, PERM_WRITE},
permission::{PermChecker, PERM_READ, PERM_WRITE},
plugin::{PluginStatus, Request, Response},
request::{Method, Router, RouterType},
route,
sea_orm::DatabaseConnection,
sea_orm::{DatabaseConnection, TransactionTrait},
service::{SResult, Service, SKYNET_SERVICE},
uuid, HyUuid, MenuItem, Skynet,
uuid,
viewer::permissions::PermissionViewer,
HyUuid, MenuItem, Skynet,
};
use skynet_api_task::{viewer::tasks::TaskViewer, ID};

Expand All @@ -41,6 +47,7 @@ include!(concat!(env!("OUT_DIR"), "/response.rs"));
db: Default::default(),
state: Default::default(),
runtime: Runtime::new().unwrap(),
manage_id: Default::default(),
})]
#[plugin_impl_root]
#[plugin_impl_call(skynet_api::plugin::api::PluginApi, skynet_api_task::Service)]
Expand All @@ -49,6 +56,7 @@ struct Plugin {
db: OnceLock<DatabaseConnection>,
state: OnceLock<Data<GlobalState>>,
runtime: Runtime,
manage_id: OnceLock<HyUuid>,
}

#[plugin_impl_trait]
Expand All @@ -67,6 +75,14 @@ impl skynet_api::plugin::api::PluginApi for Plugin {
Migrator::up(&db, None).await?;
let _ = self.db.set(db);

let tx = self.db.get().unwrap().begin().await?;
let _ = self.manage_id.set(
PermissionViewer::find_or_init(&tx, &format!("manage.{ID}"), "plugin task manager")
.await?
.id,
);
tx.commit().await?;

TaskViewer::clean_running(self.db.get().unwrap()).await?;

let _ = skynet.insert_menu(
Expand All @@ -75,17 +91,15 @@ impl skynet_api::plugin::api::PluginApi for Plugin {
plugin: Some(ID),
name: String::from("menu.task"),
path: format!("/plugin/{ID}/"),
checker: PermChecker::new_entry(
skynet.default_id[PermManagePluginID],
PERM_READ,
),
checker: PermChecker::new_entry(*self.manage_id.get().unwrap(), PERM_READ),
..Default::default()
},
1,
Some(HyUuid(uuid!("d00d36d0-6068-4447-ab04-f82ce893c04e"))),
);
let locale = Locale::new(skynet.config.lang.clone()).add_locale(i18n!("locales"));
let state = GlobalState {
memorydb: Arc::new(memorydb::default::DefaultBackend::new()),
config: Default::default(),
logger: None,
locale,
Expand All @@ -97,34 +111,35 @@ impl skynet_api::plugin::api::PluginApi for Plugin {
})
}

async fn on_register(&self, _: &Registry, skynet: Skynet, mut r: Vec<Router>) -> Vec<Router> {
async fn on_register(&self, _: &Registry, _skynet: Skynet, mut r: Vec<Router>) -> Vec<Router> {
let manage_id = *self.manage_id.get().unwrap();
r.extend(vec![
Router {
path: format!("/plugins/{ID}/tasks"),
method: Method::Get,
route: RouterType::Http(ID, String::from("api::get_all")),
checker: PermChecker::new_entry(skynet.default_id[PermManagePluginID], PERM_READ),
checker: PermChecker::new_entry(manage_id, PERM_READ),
csrf: CSRFType::Header,
},
Router {
path: format!("/plugins/{ID}/tasks"),
method: Method::Delete,
route: RouterType::Http(ID, String::from("api::delete_completed")),
checker: PermChecker::new_entry(skynet.default_id[PermManagePluginID], PERM_WRITE),
checker: PermChecker::new_entry(manage_id, PERM_WRITE),
csrf: CSRFType::Header,
},
Router {
path: format!("/plugins/{ID}/tasks/{{tid}}/output"),
method: Method::Get,
route: RouterType::Http(ID, String::from("api::get_output")),
checker: PermChecker::new_entry(skynet.default_id[PermManagePluginID], PERM_READ),
checker: PermChecker::new_entry(manage_id, PERM_READ),
csrf: CSRFType::Header,
},
Router {
path: format!("/plugins/{ID}/tasks/{{tid}}/stop"),
method: Method::Post,
route: RouterType::Http(ID, String::from("api::stop")),
checker: PermChecker::new_entry(skynet.default_id[PermManagePluginID], PERM_WRITE),
checker: PermChecker::new_entry(manage_id, PERM_WRITE),
csrf: CSRFType::Header,
},
]);
Expand Down

0 comments on commit 8d39669

Please sign in to comment.