Skip to main content

casdoor_rust_sdk/
session.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::serde_util::null_to_default;
20
21/// The name of the built-in Casdoor application.
22pub const CASDOOR_APPLICATION: &str = "app-built-in";
23/// The name of the built-in Casdoor organization.
24pub const CASDOOR_ORGANIZATION: &str = "built-in";
25
26/// Session has the same definition as
27/// <https://github.com/casdoor/casdoor/blob/master/object/session.go>.
28#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase", default)]
30pub struct Session {
31    pub owner: String,
32    pub name: String,
33    pub application: String,
34    pub created_time: String,
35
36    #[serde(deserialize_with = "null_to_default")]
37    pub session_id: Vec<String>,
38
39    #[serde(rename = "ExclusiveSignin")]
40    pub exclusive_signin: bool,
41}
42
43impl Client {
44    /// Get the sessions of the client's organization.
45    pub async fn get_sessions(&self) -> Result<Vec<Session>> {
46        self.get_list("get-sessions", &[("owner", &self.config.organization_name)])
47            .await
48    }
49
50    /// Get one page of the sessions of the client's organization, together with the total
51    /// number of the sessions.
52    pub async fn get_pagination_sessions(
53        &self,
54        p: i32,
55        page_size: i32,
56        query: &[(&str, &str)],
57    ) -> Result<(Vec<Session>, i32)> {
58        self.get_pagination(
59            "get-sessions",
60            &self.config.organization_name,
61            p,
62            page_size,
63            query,
64        )
65        .await
66    }
67
68    /// Get the session of a user in an application.
69    pub async fn get_session(&self, name: &str, application: &str) -> Result<Option<Session>> {
70        let session_pk_id = format!("{}/{}", self.get_id(name), application);
71
72        self.get_object("get-session", &[("sessionPkId", &session_pk_id)])
73            .await
74    }
75
76    /// Add a session.
77    pub async fn add_session(&self, session: &Session) -> Result<bool> {
78        Ok(self.modify_session("add-session", session, &[]).await?.1)
79    }
80
81    /// Update a session.
82    pub async fn update_session(&self, session: &Session) -> Result<bool> {
83        Ok(self.modify_session("update-session", session, &[]).await?.1)
84    }
85
86    /// Update only the given columns of a session.
87    pub async fn update_session_for_columns(
88        &self,
89        session: &Session,
90        columns: &[&str],
91    ) -> Result<bool> {
92        Ok(self
93            .modify_session("update-session", session, columns)
94            .await?
95            .1)
96    }
97
98    /// Delete a session.
99    pub async fn delete_session(&self, session: &Session) -> Result<bool> {
100        Ok(self.modify_session("delete-session", session, &[]).await?.1)
101    }
102
103    async fn modify_session(
104        &self,
105        action: &str,
106        session: &Session,
107        columns: &[&str],
108    ) -> Result<(Response, bool)> {
109        let mut session = session.clone();
110        session.owner = get_owner(&session.owner, &self.config.organization_name);
111
112        let id = format!("{}/{}", session.owner, session.name);
113        self.modify_object(action, &id, &session, columns).await
114    }
115}