account-default.go 996 字节
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package account

import (
	"errors"
	"net/http"
	"strconv"

	"github.com/eolinker/goku-api-gateway/console/module/account"
)

//DefaultAccount default
type DefaultAccount struct {
}

//NewDefaultAccount new defaultAccount
func NewDefaultAccount() *DefaultAccount {
	return &DefaultAccount{}
}

//CheckLogin 判断是否登录
func (d *DefaultAccount) CheckLogin(r *http.Request) (int, error) {

	userIDCookie, idErr := r.Cookie("userID")
	userCookie, userErr := r.Cookie("userToken")
	if idErr != nil || userErr != nil {
		e := errors.New("user not logged in")
		return 0, e
	}
	userID, err := strconv.Atoi(userIDCookie.Value)
	if err != nil {
		return 0, err
	}
	flag := account.CheckLogin(userCookie.Value, userID)
	if !flag {
		e := errors.New("illegal users")
		return userID, e
	}

	return userID, nil
}

//CheckPermission 检查操作权限
func (d *DefaultAccount) CheckPermission(pre string, isEdit bool, userID int) (bool, error) {
	if isEdit {
		return true, nil
	}
	return true, nil
}