Skip to content

Commit

Permalink
Merge pull request #30 from MuShare/develop/userInfoByUserId
Browse files Browse the repository at this point in the history
Get public userInfo by userId
  • Loading branch information
lm2343635 authored Nov 8, 2021
2 parents 9650ea1 + 5a8f0ff commit b35b5e0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@

Pluto is a JWT based authorization/authentication service. Besides providing a basic user registration and login feature, Pluto also provides a RBAC management to control the user's permission. Pluto implements the OAuth2 specified APIs for authorization.

## 微信扫码登陆

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

1. Pluto 收到微信请求,带有参数 code 和 state
2. state 是 base64 编码的 JSON 字符串,
- app: 对应 pluto app 的 name, e.g. org.mushare.easyjapanese
- redirect_url: 跳转 URL,pluto 登录成功以后跳转的 URL
3. 根据 code 进行微信登陆
4. 302 跳转到 redirectURL + "?token=...."

## Setup

Go versio: 1.16
Expand Down Expand Up @@ -59,6 +48,7 @@ Here are some helpful documents for reading.
* [Configuration](https://github.com/MuShare/pluto/blob/master/docs/configuration.md)
* [Replace Views](https://github.com/MuShare/pluto/blob/master/docs/view.md) is a guide for replacing the default html pages with your own custom files
* [JWT Token](https://github.com/MuShare/pluto/blob/master/docs/jwt.md) gives an introduction of the JWT design in Pluto.
* [WeChat Login](https://github.com/MuShare/pluto/blob/master/docs/wechat.md) gives an introduction of signing in with WeChat QRCode.

## Docker image

Expand Down
10 changes: 10 additions & 0 deletions docs/wechat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Sign in with WeChat QRCode

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

1. Pluto 收到微信请求,带有参数 code 和 state
2. state 是 base64 编码的 JSON 字符串,
- app: 对应 pluto app 的 name, e.g. org.mushare.easyjapanese
- redirect_url: 跳转 URL,pluto 登录成功以后跳转的 URL
3. 根据 code 进行微信登陆
4. 302 跳转到 redirectURL + "?token=...."
26 changes: 24 additions & 2 deletions manage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,9 +1590,9 @@ func (m *Manager) Unbind(ub *request.UnBinding, accessPayload *jwt.AccessPayload
return nil
}

func (m *Manager) PublicUserInfo(userID string) (map[string]interface{}, *perror.PlutoError) {
func (m *Manager) PublicUsersInfo(plutoId string) (map[string]interface{}, *perror.PlutoError) {

id, err := strconv.Atoi(userID)
id, err := strconv.Atoi(plutoId)
if err != nil {
return nil, perror.Forbidden
}
Expand All @@ -1617,3 +1617,25 @@ func (m *Manager) PublicUserInfo(userID string) (map[string]interface{}, *perror

return userExt.PublicInfo(), nil
}

func (m *Manager) PublicUserInfoByUserId(userId string) (*modelexts.User, *perror.PlutoError) {
user, err := models.Users(qm.Where("user_id = ?", userId)).One(m.db)
if err != nil && err == sql.ErrNoRows {
return nil, perror.UserNotExist
} else if err != nil {
return nil, perror.ServerError.Wrapper(err)
}

bindings, err := models.Bindings(qm.Where("user_id = ?", userId)).All(m.db)

if err != nil {
return nil, perror.ServerError.Wrapper(err)
}

userExt := &modelexts.User{
User: user,
Bindings: bindings,
}

return userExt, nil
}
9 changes: 8 additions & 1 deletion route/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ func (r *Router) registerUserV1Routes(prefix string) {
description: "Get user public info like avatar, name",
method: "GET",
middle: middleware.NoAuthMiddleware,
handler: r.v1.PublicUserInfo,
handler: r.v1.PublicUsersInfo,
},
{
path: "/info/{userId}/public",
description: "Get user public info like avatar, name by userId",
method: "GET",
middle: middleware.NoAuthMiddleware,
handler: r.v1.PublicUserInfoByUserId,
},
}
r.registerRoutes(routes, path.Join(prefix, "/user"), false)
Expand Down
19 changes: 17 additions & 2 deletions route/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
"net/url"

Expand Down Expand Up @@ -364,7 +365,7 @@ func (router *Router) VerifyMail(w http.ResponseWriter, r *http.Request) *perror
return nil
}

func (router *Router) PublicUserInfo(w http.ResponseWriter, r *http.Request) *perror.PlutoError {
func (router *Router) PublicUsersInfo(w http.ResponseWriter, r *http.Request) *perror.PlutoError {

pui := request.PublicUserInfos{}

Expand All @@ -374,7 +375,7 @@ func (router *Router) PublicUserInfo(w http.ResponseWriter, r *http.Request) *pe

res := make(map[string]map[string]interface{})
for _, id := range pui.IDs {
info, perr := router.manager.PublicUserInfo(id)
info, perr := router.manager.PublicUsersInfo(id)

if perr != nil {
continue
Expand All @@ -387,3 +388,17 @@ func (router *Router) PublicUserInfo(w http.ResponseWriter, r *http.Request) *pe

return nil
}

func (router *Router) PublicUserInfoByUserId(w http.ResponseWriter, r *http.Request) *perror.PlutoError {
vars := mux.Vars(r)
userId := vars["userId"]
res, perr := router.manager.PublicUserInfoByUserId(userId)

if perr != nil {
return perr
}

routeUtils.ResponseOK(res.PublicInfo(), w)

return nil
}

0 comments on commit b35b5e0

Please sign in to comment.