69 lines
1.6 KiB
Go
Raw Normal View History

2021-02-14 00:22:24 +08:00
// Copyright 2021 The casbin 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
2021-08-28 11:13:38 +08:00
import (
"strconv"
"github.com/astaxie/beego"
)
2021-08-08 16:00:19 +08:00
// ResponseOk ...
func (c *ApiController) ResponseOk(data ...interface{}) {
resp := Response{Status: "ok"}
switch len(data) {
case 2:
resp.Data2 = data[1]
fallthrough
case 1:
resp.Data = data[0]
}
c.Data["json"] = resp
c.ServeJSON()
}
2021-08-07 22:02:56 +08:00
// ResponseError ...
func (c *ApiController) ResponseError(error string, data ...interface{}) {
resp := Response{Status: "error", Msg: error}
switch len(data) {
case 2:
resp.Data2 = data[1]
fallthrough
case 1:
resp.Data = data[0]
}
c.Data["json"] = resp
c.ServeJSON()
}
2021-05-17 23:25:28 +08:00
2021-08-07 22:02:56 +08:00
// RequireSignedIn ...
2021-05-17 23:25:28 +08:00
func (c *ApiController) RequireSignedIn() (string, bool) {
userId := c.GetSessionUsername()
2021-05-17 23:25:28 +08:00
if userId == "" {
2021-08-08 11:06:45 +08:00
c.ResponseError("Please sign in first")
2021-05-17 23:25:28 +08:00
return "", false
}
return userId, true
}
2021-08-28 11:13:38 +08:00
func getInitScore() int {
score, err := strconv.Atoi(beego.AppConfig.String("initScore"))
if err != nil {
panic(err)
}
return score
}