Beispiel #1
0
// temp function
func (s *TimeZoneService) UserTimeZoneDontCheckCookie(request *http.Request) *model.TimeZoneInfo {
	// Check TimeZone settings.
	sessionId := sessions.GetSessionId(request)
	if tzi := sessions.Get(sessionId, TimeZoneKey); tzi != nil {
		if timezoneinfo, ok := (tzi).(*model.TimeZoneInfo); ok {
			return timezoneinfo
		} else {
			log.Printf("Warrning! TimeZone in session is not type model.TimeZoneInfo!!")
		}
	} else {

		// 有这个else, 每次重启都需要登陆。否则不需要登陆,会记录cookie。

		// if not, check long live session.i.e. cookie.
		cookies := sessions.LongCookieSession(request)
		if offset, ok := cookies.Values[TimeZoneKey]; ok && nil != offset {
			if offsetInt, ok := offset.(int); ok {
				// if cookie has timezone, pass. Set to session
				sessions.Set(sessionId, TimeZoneKey, model.NewTimeZoneInfo(offsetInt))
			} else {
				log.Printf("Warrning! Offset in cookie are not int, offset: %v", offset)
			}
		} else {
			// 这个版本不redirect到登陆页面。因为调用者已经在登陆页面了。
			// if not found in cookie; jump to exception page.
			// fmt.Println("\n\n\njjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj")
			// panic(exception.NewTimeZoneNotFoundErrorf("TimeZoneInfo not found in session."))
		}

	}
	return EmptyTimeZone
}
Beispiel #2
0
// set UserToken to session.
func (s *UserService) setToSession(w http.ResponseWriter, r *http.Request, userToken *model.UserToken) {
	session := sessions.LongCookieSession(r)
	session.Values[USER_TOKEN_SESSION_KEY] = userToken
	if err := session.Save(r, w); err != nil {
		fmt.Println("======= ERROR TO HANDLE ========================")
		fmt.Println(err)
	}
}
Beispiel #3
0
func (s *TimeZoneService) SaveTimeZone(response http.ResponseWriter, request *http.Request,
	timezone *model.TimeZoneInfo) {
	// set both cookie and session
	sessionId := sessions.GetSessionId(request)
	sessions.Set(sessionId, TimeZoneKey, timezone)
	cookies := sessions.LongCookieSession(request)
	cookies.Values[TimeZoneKey] = timezone.Offset
	cookies.Save(request, response)
	// fmt.Println("Set timezone to ", offset, " to session : ", sessionId)
}
Beispiel #4
0
func HasAnyRole(w http.ResponseWriter, r *http.Request, roles ...string) bool {
	session := sessions.LongCookieSession(r)
	if userTokenRaw, ok := session.Values[config.USER_TOKEN_SESSION_KEY]; ok && userTokenRaw != nil {
		if userToken := userTokenRaw.(*model.UserToken); userToken != nil {
			// TODO: check if userToken is outdated.
			if outdated := false; !outdated {
				// TODO: update userToken.Tiemout
				// userToken := service.UserService.GetLogin(w, r)
				if userToken.Roles != nil {
					for _, requiredRole := range roles {
						requiredRole = strings.ToLower(requiredRole)
						for _, role := range userToken.Roles {
							if strings.ToLower(role) == requiredRole {
								return true
							}
						}
					}
				}

			}
		}
	}
	return false
}
Beispiel #5
0
// will be very fast.
// return true if user is login and login is available.
// return false if
func (s *UserService) GetLogin(w http.ResponseWriter, r *http.Request) *model.UserToken {
	if s.logs.Trace() {
		s.logs.Printf("Enter function: GetLogin [session]")
	}

	session := sessions.LongCookieSession(r)

	// session.Values["55667788"] = &model.UserToken{Name: "84983"}
	// session.Values["55667788--"] = "&model.UserToken{Name: 84983}"
	// session.Save(r, w)
	// for k, v := range session.Values {
	// 	fmt.Println("\t)))->:", k, " -> ", v)
	// }

	fmt.Println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")

	// deep trace.
	if DEEP_TRACE && s.logs.Trace() {
		s.logs.Printf("  DEEP TRACE: everything in long-session: %s {", session.ID)
		for k, v := range session.Values {
			s.logs.Printf("  DEEP TRACE: %v : %v", k, v)
		}
		s.logs.Printf("  }")
	}

	if userTokenRaw, ok := session.Values[config.USER_TOKEN_SESSION_KEY]; ok && userTokenRaw != nil {
		if s.logs.Trace() {
			s.logs.Printf("Got userToken : %v.", userTokenRaw)
		}

		if userToken := userTokenRaw.(*model.UserToken); userToken != nil {
			if s.logs.Trace() {
				s.logs.Printf("Got userToken.outdated = %v.", "TODO: check if userToken is outdated.")
			}

			// TODO: check if userToken is outdated.
			if outdated := false; !outdated {
				// TODO: update userToken.Tiemout
				if s.logs.Trace() {
					s.logs.Printf("GetLogin success, return cached user.")
				}
				return userToken
			}
		}
	}

	// if not in session, try cookie
	if userToken, err := s.LoginFromCookie(r); err != nil {
		if s.logs.Error() {
			s.logs.Printf("Login from cookie failed, reason: %s\n", err.Error())
		}
		return nil
	} else {
		if s.logs.Info() {
			s.logs.Printf("[Cookie] Login from cookie succeed, username: %s, password(hash): %s\n",
				userToken.Username, userToken.Password)
		}

		// if success, update it to session.
		s.setToSession(w, r, userToken)
		if s.logs.Trace() {
			s.logs.Printf("GetLogin success, return user.")
		}
		return userToken
	}
}
Beispiel #6
0
func (s *UserService) removeUserTokenSession(w http.ResponseWriter, r *http.Request) {
	session := sessions.LongCookieSession(r)
	session.Values[USER_TOKEN_SESSION_KEY] = nil
	delete(session.Values, USER_TOKEN_SESSION_KEY)
	session.Save(r, w)
}