mirror of https://github.com/casbin/casnode.git
Add account API.
This commit is contained in:
parent
fa64e4ca6d
commit
ef179a5d91
|
@ -16,5 +16,10 @@
|
|||
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
tmp/
|
||||
tmpFiles/
|
||||
*.tmp
|
||||
tmp/
|
||||
logs/
|
||||
lastupdate.tmp
|
||||
commentsRouter*.go
|
|
@ -0,0 +1,162 @@
|
|||
// Copyright 2020 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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/casbin/casbin-forum/object"
|
||||
"github.com/casbin/casbin-forum/util"
|
||||
)
|
||||
|
||||
type SignupForm struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Status string `json:"status"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Data2 interface{} `json:"data2"`
|
||||
}
|
||||
|
||||
// @Title Signup
|
||||
// @Description sign up a new member
|
||||
// @Param username formData string true "The username to sign up"
|
||||
// @Param password formData string true "The password"
|
||||
// @Success 200 {object} controllers.api_controller.Response The Response object
|
||||
// @router /signup [post]
|
||||
func (c *APIController) Signup() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() != "" {
|
||||
resp = Response{Status: "error", Msg: "errorSignoutBeforeSignup", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var form SignupForm
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
member, password, email := form.Username, form.Password, form.Email
|
||||
|
||||
msg := object.CheckMemberSignup(member, password)
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg, Data: ""}
|
||||
} else {
|
||||
member := &object.Member{
|
||||
Id: member,
|
||||
Password: password,
|
||||
Email: email,
|
||||
}
|
||||
object.AddMember(member)
|
||||
|
||||
//c.SetSessionUser(user)
|
||||
|
||||
util.LogInfo(c.Ctx, "API: [%s] is signed up as new member", member)
|
||||
resp = Response{Status: "ok", Msg: "success", Data: member}
|
||||
}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Signin
|
||||
// @Description sign in as a member
|
||||
// @Param username formData string true "The username to sign in"
|
||||
// @Param password formData string true "The password"
|
||||
// @Success 200 {object} controllers.api_controller.Response The Response object
|
||||
// @router /signin [post]
|
||||
func (c *APIController) Signin() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() != "" {
|
||||
resp = Response{Status: "error", Msg: "errorSignoutBeforeSignin", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var form SignupForm
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var msg string
|
||||
var member string
|
||||
var password string
|
||||
member, password = form.Username, form.Password
|
||||
msg = object.CheckMemberLogin(member, password)
|
||||
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg, Data: ""}
|
||||
} else {
|
||||
c.SetSessionUser(member)
|
||||
|
||||
util.LogInfo(c.Ctx, "API: [%s] signed in", member)
|
||||
resp = Response{Status: "ok", Msg: "success", Data: member}
|
||||
}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Signout
|
||||
// @Description sign out the current member
|
||||
// @Success 200 {object} controllers.api_controller.Response The Response object
|
||||
// @router /signout [post]
|
||||
func (c *APIController) Signout() {
|
||||
var resp Response
|
||||
|
||||
member := c.GetSessionUser()
|
||||
util.LogInfo(c.Ctx, "API: [%s] signed out", member)
|
||||
|
||||
c.SetSessionUser("")
|
||||
|
||||
resp = Response{Status: "ok", Msg: "success", Data: member}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *APIController) GetAccount() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() == "" {
|
||||
resp = Response{Status: "error", Msg: "errorSigninFirst", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var memberObj interface{}
|
||||
username := c.GetSessionUser()
|
||||
memberObj = object.GetMember(username)
|
||||
resp = Response{Status: "ok", Msg: "", Data: util.StructToJson(memberObj)}
|
||||
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *APIController) GetSessionId() {
|
||||
c.Data["json"] = c.StartSession().SessionID()
|
||||
c.ServeJSON()
|
||||
}
|
|
@ -19,3 +19,16 @@ import "github.com/astaxie/beego"
|
|||
type APIController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *APIController) GetSessionUser() string {
|
||||
user := c.GetSession("username")
|
||||
if user == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return user.(string)
|
||||
}
|
||||
|
||||
func (c *APIController) SetSessionUser(user string) {
|
||||
c.SetSession("username", user)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2020 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 object
|
||||
|
||||
func HasMember(memberId string) bool {
|
||||
return GetMember(memberId) != nil
|
||||
}
|
||||
|
||||
func IsPasswordCorrect(memberId string, password string) bool {
|
||||
objMember := GetMember(memberId)
|
||||
return objMember.Password == password
|
||||
}
|
||||
|
||||
func CheckMemberSignup(member string, password string) string {
|
||||
if len(member) == 0 || len(password) == 0 {
|
||||
return "errorUsernameOrPasswordEmpty"
|
||||
} else if HasMember(member) {
|
||||
return "errorUsernameExisted"
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func CheckMemberLogin(member string, password string) string {
|
||||
if !HasMember(member) {
|
||||
return "errorUsernameNotFound"
|
||||
}
|
||||
|
||||
if !IsPasswordCorrect(member, password) {
|
||||
return "errorPasswordWrong"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
|
@ -16,6 +16,7 @@ package object
|
|||
|
||||
type Member struct {
|
||||
Id string `xorm:"varchar(100) notnull pk" json:"id"`
|
||||
Password string `xorm:"varchar(100) notnull" json:"-"`
|
||||
No int `json:"no"`
|
||||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
||||
Phone string `xorm:"varchar(100)" json:"phone"`
|
||||
|
|
|
@ -56,4 +56,9 @@ func initAPI() {
|
|||
beego.Router("/api/update-node", &controllers.APIController{}, "POST:UpdateNode")
|
||||
beego.Router("/api/add-node", &controllers.APIController{}, "POST:AddNode")
|
||||
beego.Router("/api/delete-node", &controllers.APIController{}, "POST:DeleteNode")
|
||||
|
||||
beego.Router("/api/signup", &controllers.APIController{}, "POST:Signup")
|
||||
beego.Router("/api/signin", &controllers.APIController{}, "POST:Signin")
|
||||
beego.Router("/api/signout", &controllers.APIController{}, "POST:Signout")
|
||||
beego.Router("/api/get-account", &controllers.APIController{}, "GET:GetAccount")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2020 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 util
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func StructToJson(v interface{}) string {
|
||||
//data, err := json.MarshalIndent(v, "", " ")
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return string(data)
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright 2020 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 util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/context"
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
func GetIPInfo(clientIP string) string {
|
||||
if clientIP == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
ips := strings.Split(clientIP, ",")
|
||||
res := ""
|
||||
for i := range ips {
|
||||
ip := strings.TrimSpace(ips[i])
|
||||
desc := "" // GetDescFromIP(ip)
|
||||
ipstr := fmt.Sprintf("%s: %s", ip, desc)
|
||||
if i != len(ips) - 1 {
|
||||
res += ipstr + " -> "
|
||||
} else {
|
||||
res += ipstr
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func getIPFromRequest(req *http.Request) string {
|
||||
clientIP := req.Header.Get("x-forwarded-for")
|
||||
if clientIP == "" {
|
||||
ipPort := strings.Split(req.RemoteAddr, ":")
|
||||
if len(ipPort) >= 1 && len(ipPort) <= 2 {
|
||||
clientIP = ipPort[0]
|
||||
} else if len(ipPort) > 2 {
|
||||
idx := strings.LastIndex(req.RemoteAddr, ":")
|
||||
clientIP = req.RemoteAddr[0:idx]
|
||||
clientIP = strings.TrimLeft(clientIP, "[")
|
||||
clientIP = strings.TrimRight(clientIP, "]")
|
||||
}
|
||||
}
|
||||
|
||||
return GetIPInfo(clientIP)
|
||||
}
|
||||
|
||||
func LogInfo(ctx *context.Context, f string, v ...interface{}) {
|
||||
ipString := fmt.Sprintf("(%s) ", getIPFromRequest(ctx.Request))
|
||||
logs.Info(ipString + f, v...)
|
||||
}
|
||||
|
||||
func LogWarning(ctx *context.Context, f string, v ...interface{}) {
|
||||
ipString := fmt.Sprintf("(%s) ", getIPFromRequest(ctx.Request))
|
||||
logs.Warning(ipString + f, v...)
|
||||
}
|
||||
|
||||
func ReadLog() []string {
|
||||
f, err := os.Open("logs/casbin-forum.log")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return strings.Split(string(bytes), "\n")
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2020 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.
|
||||
|
||||
import * as Setting from "../Setting";
|
||||
|
||||
export function getAccount() {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-account`, {
|
||||
method: 'GET',
|
||||
credentials: 'include'
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function signup(values) {
|
||||
return fetch(`${Setting.ServerUrl}/api/signup`, {
|
||||
method: 'POST',
|
||||
credentials: "include",
|
||||
body: JSON.stringify(values),
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function signin(values) {
|
||||
return fetch(`${Setting.ServerUrl}/api/signin`, {
|
||||
method: 'POST',
|
||||
credentials: "include",
|
||||
body: JSON.stringify(values),
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function signout() {
|
||||
return fetch(`${Setting.ServerUrl}/api/signout`, {
|
||||
method: 'POST',
|
||||
credentials: "include",
|
||||
}).then(res => res.json());
|
||||
}
|
Loading…
Reference in New Issue