Exemple #1
0
// update login time
func (us *UserService) updateLoginTime(u *model.User) error {
	u.LastLoginTime = time.Now().Unix()
	if _, err := core.Db.Exec("UPDATE user SET last_login_time = ? WHERE id = ?", u.LastLoginTime, u.Id); err != nil {
		return err
	}
	return nil
}
Exemple #2
0
// authorize user
func (us *UserService) Authorize(v interface{}) (*Result, error) {
	opt, ok := v.(UserAuthOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(us.Authorize, opt)
	}
	var (
		user *model.User
		err  error
	)

	// get user
	if opt.Name != "" {
		if user, err = getUserBy("name", opt.Name); err != nil {
			return nil, err
		}
	} else if opt.Email != "" {
		if user, err = getUserBy("email", opt.Email); err != nil {
			return nil, err
		}
	}
	if user == nil {
		return nil, ErrUserNotFound
	}
	if !user.IsAccessible() {
		return nil, ErrUserNotAccess
	}
	if !user.IsPassword(opt.Password) {
		return nil, ErrUserWrongPassword
	}

	// create token
	var token *model.UserToken
	if token, err = us.createToken(user, opt); err != nil {
		return nil, err
	}

	// update login time
	if err = us.updateLoginTime(user); err != nil {
		return nil, err
	}

	res := newResult(us.Authorize)
	res.Set(user, token)
	return res, nil
}