Skip to content

Commit

Permalink
v1.4 增加userInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
何全 committed Dec 24, 2019
1 parent e305d9e commit cb04f83
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Go Web Admin

![构建](https://travis-ci.org/hequan2017/go-admin.svg?branch=master)
![版本](https://img.shields.io/badge/release-1.2.2-blue.svg)
![版本](https://img.shields.io/badge/release-1.4.0-blue.svg)
![语言](https://img.shields.io/badge/language-goland1.2-blue.svg)
![base](https://img.shields.io/badge/base-gin-blue.svg)
![base](https://img.shields.io/badge/base-casbin-blue.svg)
Expand Down Expand Up @@ -66,7 +66,7 @@ hequan 运维部
用户 admin 有所有的权限,不进行权限匹配
登录接口 /auth 不进行验证
登录接口 /auth /api/v1/userInfo 不进行验证
```

## 请求
Expand Down
3 changes: 2 additions & 1 deletion conf/rbac_model.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ m = g(r.sub, p.sub) == true \
&& keyMatch2(r.obj, p.obj) == true \
&& regexMatch(r.act, p.act) == true \
|| r.sub == "admin" \
|| keyMatch2(r.obj, "/auth") == true \
|| keyMatch2(r.obj, "/auth") == true \
|| keyMatch2(r.obj, "/api/v1/userInfo") == true
4 changes: 2 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-12-24 12:19:55.4766668 +0800 CST m=+0.090628501
// 2019-12-24 14:55:53.8650092 +0800 CST m=+0.083769601

package docs

Expand All @@ -23,7 +23,7 @@ var doc = `{
"name": "MIT",
"url": "https://github.com/hequan2017/go-admin/blob/master/LICENSE"
},
"version": "1.2.2"
"version": "1.4.0"
},
"host": "127.0.0.1:8000",
"paths": {
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "MIT",
"url": "https://github.com/hequan2017/go-admin/blob/master/LICENSE"
},
"version": "1.2.2"
"version": "1.4.0"
},
"host": "127.0.0.1:8000",
"paths": {
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ info:
url: https://github.com/hequan2017/go-admin/blob/master/LICENSE
termsOfService: https://github.com/hequan2017/go-admin
title: go-admin
version: 1.2.2
version: 1.4.0
paths:
/api/v1/menus:
get:
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// @title go-admin
// @version 1.2.2
// @version 1.4.0
// @description go-admin
// @termsOfService https://github.com/hequan2017/go-admin

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Claims struct {

func GenerateToken(username, password string) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(3 * time.Hour)
expireTime := nowTime.Add(24 * time.Hour)

claims := Claims{
username,
Expand Down
10 changes: 6 additions & 4 deletions routers/api/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"fmt"
"github.com/astaxie/beego/validation"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -134,10 +133,14 @@ func GetUserInfo(c *gin.Context) {
appG := app.Gin{C: c}
Authorization := c.GetHeader("Authorization")
token := strings.Split(Authorization, " ")
t, _ := jwt.Parse(token[1], func(*jwt.Token) (interface{}, error) {
t, err := jwt.Parse(token[1], func(*jwt.Token) (interface{}, error) {
return jwtGet.JwtSecret, nil
})
fmt.Print(jwtGet.GetIdFromClaims("username", t.Claims))

if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH, nil)
return
}

userService := user_service.User{
Username: jwtGet.GetIdFromClaims("username", t.Claims),
Expand Down Expand Up @@ -170,7 +173,6 @@ func GetUserInfo(c *gin.Context) {
for _, v2 := range r.Menu {
menus = append(menus, v2.Name)
}
fmt.Print(menus)
}
menus = util.RemoveRepByMap(menus)
v.Password = strings.Join(menus, ",")
Expand Down
1 change: 0 additions & 1 deletion routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func InitRouter() *gin.Engine {
apiV1 := r.Group("/api/v1")

apiV1.Use(jwt.JWT()) // token 验证

apiV1.GET("/userInfo", api.GetUserInfo)
apiV1.Use(permission.CasbinMiddleware()) // 权限 验证

Expand Down
10 changes: 10 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
package main

import (
"fmt"
)

func main() {
fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
fruitslice := fruitarray[2:3]
fmt.Printf("length of slice %d capacity %d", len(fruitslice), cap(fruitslice)) // length of is 2 and capacity is 6
}

0 comments on commit cb04f83

Please sign in to comment.