2023-01-06 15:04:13 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/beego/beego/utils/pagination"
|
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetSessions
|
|
|
|
// @Title GetSessions
|
|
|
|
// @Tag Session API
|
2023-02-12 09:33:24 +08:00
|
|
|
// @Description Get organization user sessions.
|
2023-01-06 15:04:13 +08:00
|
|
|
// @Param owner query string true "The organization name"
|
|
|
|
// @Success 200 {array} string The Response object
|
|
|
|
// @router /get-sessions [get]
|
|
|
|
func (c *ApiController) GetSessions() {
|
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
|
|
|
owner := c.Input().Get("owner")
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2023-01-06 15:04:13 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
sessions, err := object.GetSessions(owner)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(sessions)
|
2023-01-06 15:04:13 +08:00
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-05-30 15:49:39 +08:00
|
|
|
count, err := object.GetSessionCount(owner, field, value)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
|
|
|
sessions, err := object.GetPaginationSessions(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:04:13 +08:00
|
|
|
c.ResponseOk(sessions, paginator.Nums())
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 09:33:24 +08:00
|
|
|
|
|
|
|
// GetSingleSession
|
|
|
|
// @Title GetSingleSession
|
|
|
|
// @Tag Session API
|
|
|
|
// @Description Get session for one user in one application.
|
|
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
|
|
// @Success 200 {array} string The Response object
|
|
|
|
// @router /get-session [get]
|
|
|
|
func (c *ApiController) GetSingleSession() {
|
|
|
|
id := c.Input().Get("sessionPkId")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
session, err := object.GetSingleSession(id)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(session)
|
2023-02-12 09:33:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSession
|
|
|
|
// @Title UpdateSession
|
|
|
|
// @Tag Session API
|
|
|
|
// @Description Update session for one user in one application.
|
|
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
|
|
// @Success 200 {array} string The Response object
|
|
|
|
// @router /update-session [post]
|
|
|
|
func (c *ApiController) UpdateSession() {
|
|
|
|
var session object.Session
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdateSession(util.GetSessionId(session.Owner, session.Name, session.Application), &session))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSession
|
|
|
|
// @Title AddSession
|
|
|
|
// @Tag Session API
|
|
|
|
// @Description Add session for one user in one application. If there are other existing sessions, join the session into the list.
|
|
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
|
|
// @Param sessionId query string true "sessionId to be added"
|
|
|
|
// @Success 200 {array} string The Response object
|
|
|
|
// @router /add-session [post]
|
|
|
|
func (c *ApiController) AddSession() {
|
|
|
|
var session object.Session
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.AddSession(&session))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSession
|
|
|
|
// @Title DeleteSession
|
|
|
|
// @Tag Session API
|
|
|
|
// @Description Delete session for one user in one application.
|
|
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
|
|
// @Success 200 {array} string The Response object
|
|
|
|
// @router /delete-session [post]
|
|
|
|
func (c *ApiController) DeleteSession() {
|
|
|
|
var session object.Session
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &session)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteSession(util.GetSessionId(session.Owner, session.Name, session.Application)))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSessionDuplicated
|
|
|
|
// @Title IsSessionDuplicated
|
|
|
|
// @Tag Session API
|
|
|
|
// @Description Check if there are other different sessions for one user in one application.
|
|
|
|
// @Param id query string true "The id(organization/application/user) of session"
|
|
|
|
// @Param sessionId query string true "sessionId to be checked"
|
|
|
|
// @Success 200 {array} string The Response object
|
2023-02-13 18:16:31 +08:00
|
|
|
// @router /is-session-duplicated [get]
|
2023-02-12 09:33:24 +08:00
|
|
|
func (c *ApiController) IsSessionDuplicated() {
|
|
|
|
id := c.Input().Get("sessionPkId")
|
|
|
|
sessionId := c.Input().Get("sessionId")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
isUserSessionDuplicated, err := object.IsSessionDuplicated(id, sessionId)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-23 09:49:16 +08:00
|
|
|
c.ResponseOk(isUserSessionDuplicated)
|
2023-02-12 09:33:24 +08:00
|
|
|
}
|