Exemplo n.º 1
0
func GetSession(w rest.ResponseWriter, r *rest.Request) (model.UserWithToken, error) {
	session, err := store.Get(r.Request, "user-authentication")
	var user model.UserWithToken
	if err != nil {
		return user, err
	}
	if session.Values["ID"] != nil {
		user.ID = session.Values["ID"].(uint)
		user.Username = session.Values["Username"].(string)
		user.Name = session.Values["Name"].(string)
		user.Role = session.Values["Role"].(string)
		user.AccessToken = session.Values["AccessToken"].(string)
	}
	return user, nil
}
Exemplo n.º 2
0
func VerifyAccessToken(token string, ctx context.Context, isVerifyAdministratorRole bool) (model.UserWithToken, error) {
	var user model.UserWithToken
	token = strings.TrimSpace(token)
	if token == "" {
		return user, errors.New("AccessToken 不能为空!")
	}
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		return user, errors.New("内部服务器错误")
	}

	count, err := repo.CountUserAccessTokenByToken(token)
	if err != nil {
		return user, err
	}

	if count != 1 {
		return user, errors.New("AccessToken 不正确!")
	}

	userInfo, err := repo.GetUserByAccessToken(token)
	if err != nil {
		return user, err
	}

	if isVerifyAdministratorRole == true {
		if userInfo.Role != "Administrator" {
			return user, errors.New("权限不足,请使用超级管理员账号登录!")
		}
	}

	user.ID = userInfo.ID
	user.Username = userInfo.Username
	user.Name = userInfo.Name
	user.Role = userInfo.Role
	user.AccessToken = userInfo.AccessToken
	return user, nil
}