96 lines
2.1 KiB
Go
Raw Normal View History

2021-03-13 23:06:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
2020-10-20 21:57:29 +08:00
//
// 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 (
"time"
"github.com/astaxie/beego"
"github.com/casbin/casdoor/util"
)
2020-10-20 21:57:29 +08:00
// controller for handlers under /api uri
2020-10-20 21:57:29 +08:00
type ApiController struct {
beego.Controller
}
// controller for handlers directly under / (root)
type RootController struct {
ApiController
}
type SessionData struct {
ExpireTime int64
}
2021-08-07 22:02:56 +08:00
// GetSessionUsername ...
func (c *ApiController) GetSessionUsername() string {
// check if user session expired
sessionData := c.GetSessionData()
if sessionData != nil &&
sessionData.ExpireTime != 0 &&
sessionData.ExpireTime < time.Now().Unix() {
c.SetSessionUsername("")
c.SetSessionData(nil)
return ""
}
2020-10-20 23:14:03 +08:00
user := c.GetSession("username")
if user == nil {
return ""
}
return user.(string)
}
2021-08-07 22:02:56 +08:00
// SetSessionUsername ...
func (c *ApiController) SetSessionUsername(user string) {
2020-10-20 23:14:03 +08:00
c.SetSession("username", user)
2020-10-20 21:57:29 +08:00
}
2021-03-28 00:48:34 +08:00
2021-08-07 22:02:56 +08:00
// GetSessionData ...
func (c *ApiController) GetSessionData() *SessionData {
session := c.GetSession("SessionData")
if session == nil {
return nil
}
sessionData := &SessionData{}
err := util.JsonToStruct(session.(string), sessionData)
if err != nil {
panic(err)
}
return sessionData
}
2021-08-07 22:02:56 +08:00
// SetSessionData ...
func (c *ApiController) SetSessionData(s *SessionData) {
if s == nil {
c.DelSession("SessionData")
return
}
c.SetSession("SessionData", util.StructToJson(s))
}
2021-03-28 00:48:34 +08:00
func wrapActionResponse(affected bool) *Response {
if affected {
2021-03-28 08:59:12 +08:00
return &Response{Status: "ok", Msg: "", Data: "Affected"}
2021-03-28 00:48:34 +08:00
} else {
2021-03-28 08:59:12 +08:00
return &Response{Status: "ok", Msg: "", Data: "Unaffected"}
2021-03-28 00:48:34 +08:00
}
}