user.go 4.6 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8
package account

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

Y
Your Name 已提交
9 10
	goku_handler "github.com/eolinker/goku-api-gateway/goku-handler"

黄孟柱 已提交
11 12 13
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/account"
	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
14 15
)

Y
Your Name 已提交
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
//OperationUser 用户权限
const OperationUser = "user"

//UserController 用户控制器
type UserController struct {
}

//NewUserController 新建用户控制器
func NewUserController() *UserController {
	return &UserController{}
}

//Handlers 处理类
func (u *UserController) Handlers(factory *goku_handler.AccountHandlerFactory) map[string]http.Handler {

	return map[string]http.Handler{
		"/logout":            factory.NewAccountHandleFunction(OperationUser, false, Logout),
		"/password/edit":     factory.NewAccountHandleFunction(OperationUser, false, EditPassword),
		"/getInfo":           factory.NewAccountHandleFunction(OperationUser, false, GetUserInfo),
		"/getUserType":       factory.NewAccountHandleFunction(OperationUser, false, GetUserType),
		"/checkIsAdmin":      factory.NewAccountHandleFunction(OperationUser, false, CheckUserIsAdmin),
		"/checkIsSuperAdmin": factory.NewAccountHandleFunction(OperationUser, false, CheckUserIsSuperAdmin),
	}
}

Y
Your Name 已提交
41
//Logout 用户注销
E
eoLinker API Management 已提交
42 43 44 45 46 47 48 49 50 51 52
func Logout(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	userIDCookie := http.Cookie{Name: "userID", Path: "/", MaxAge: -1}
	userCookie := http.Cookie{Name: "userToken", Path: "/", MaxAge: -1}
	http.SetCookie(httpResponse, &userIDCookie)
	http.SetCookie(httpResponse, &userCookie)

	controller.WriteResultInfo(httpResponse, "user", "", nil)
	return
}

Y
Your Name 已提交
53
//EditPassword 修改账户信息
E
eoLinker API Management 已提交
54 55 56 57 58 59 60 61 62
func EditPassword(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	oldPassword := httpRequest.PostFormValue("oldPassword")
	newPassword := httpRequest.PostFormValue("newPassword")
	if flag, _ := regexp.MatchString("^[0-9a-zA-Z]{32}$", oldPassword); !flag {

		controller.WriteError(httpResponse,
			"110005",
			"user",
Y
Your Name 已提交
63 64
			"[ERROR]Illegal oldPassword!",
			errors.New("[ERROR]Illegal oldPassword"))
E
eoLinker API Management 已提交
65 66 67 68 69 70 71
		return
	}
	if flag, _ := regexp.MatchString("^[0-9a-zA-Z]{32}$", newPassword); !flag {

		controller.WriteError(httpResponse,
			"110006",
			"user",
Y
Your Name 已提交
72 73
			"[ERROR]Illegal newPassword!",
			errors.New("[ERROR]Illegal newPassword"))
E
eoLinker API Management 已提交
74 75
		return
	}
Y
Your Name 已提交
76
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	flag, result, err := account.EditPassword(oldPassword, newPassword, userID)
	if !flag {
		controller.WriteError(httpResponse,
			"120000",
			"user",
			result,
			err)
		return
	}

	userCookie := &http.Cookie{Name: "userToken", Value: utils.Md5(result + utils.Md5(newPassword)), Path: "/", MaxAge: 86400}
	nameCookie := &http.Cookie{Name: "userID", Value: strconv.Itoa(userID), Path: "/", MaxAge: 86400}
	http.SetCookie(httpResponse, userCookie)
	http.SetCookie(httpResponse, nameCookie)

	controller.WriteResultInfo(httpResponse, "user", "", nil)

	return
}

Y
Your Name 已提交
97
//GetUserInfo 获取用户信息
E
eoLinker API Management 已提交
98
func GetUserInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
99
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112

	flag, result, err := account.GetUserInfo(userID)
	if !flag {

		controller.WriteError(httpResponse, "110000", "user", result.(string), err)
		return
	}

	controller.WriteResultInfo(httpResponse, "user", "userInfo", result)

	return
}

Y
Your Name 已提交
113
//GetUserType 获取用户类型
E
eoLinker API Management 已提交
114 115
func GetUserType(httpResponse http.ResponseWriter, httpRequest *http.Request) {

Y
Your Name 已提交
116
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
117 118 119 120 121 122 123 124 125 126 127
	flag, result, err := account.GetUserType(userID)
	if !flag {

		controller.WriteError(httpResponse, "110000", "user", result.(string), err)
		return
	}
	controller.WriteResultInfo(httpResponse, "user", "userType", result)

	return
}

Y
Your Name 已提交
128
//CheckUserIsAdmin 判断是否是管理员
E
eoLinker API Management 已提交
129
func CheckUserIsAdmin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
130
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	flag, _, err := account.CheckUserIsAdmin(userID)
	if !flag {

		controller.WriteError(httpResponse,
			"110000",
			"user",
			"This is not administrator",
			err)
		return

	}

	controller.WriteResultInfo(httpResponse, "user", "", nil)
	return
}

Y
Your Name 已提交
148
//CheckUserIsSuperAdmin 判断是否是超级管理员
E
eoLinker API Management 已提交
149
func CheckUserIsSuperAdmin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
150
	userID := goku_handler.UserIDFromRequest(httpRequest)
E
eoLinker API Management 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

	flag, _, err := account.CheckUserIsSuperAdmin(userID)

	if !flag {

		controller.WriteError(httpResponse,
			"110000",
			"user",
			"This is not administrator",
			err)
		return

	}

	controller.WriteResultInfo(httpResponse, "user", "", nil)
	return
}