func (self *MySession) Destroy() (bool, error) { if len(self.key) == 0 || len(self.sessionId) == 0 { errorMsg := fmt.Sprintln("Uninitialized yet!") return false, errors.New(errorMsg) } go_lib.LogInfof("Destroy session (key=%s)... \n", self.key) grantors, err := dao.GetHash(self.key, SESSION_GRANTORS_KEY) if err != nil { return false, err } _, err = dao.DelKey(self.key) if err != nil { return false, err } if len(grantors) > 0 { _, err = dao.DelHashField(SESSION_MAP_KEY, grantors) if err != nil { return false, err } } go_lib.LogInfof("Delete session cookie (value=%s)...\n", self.sessionId) hmSessionCookie.Delete(SESSION_COOKIE_KEY, self.w) go_lib.LogInfof("The session (key=%s) is destroyed. \n", self.key) return true, nil }
func GetMatchedSession(w http.ResponseWriter, r *http.Request) (*MySession, error) { sessionId := hmSessionCookie.GetOne(SESSION_COOKIE_KEY, r) if len(sessionId) == 0 { warningMsg := fmt.Sprintf("Not found matched session! No session cookie!") go_lib.LogWarnln(warningMsg) return nil, nil } sessionkey := generateSessionKey(sessionId) exists, err := dao.Exists(sessionkey) if err != nil { return nil, err } if !exists { warningMsg := fmt.Sprintf("Not found matched session! No session in storage! (sessionId=%s, sessionKey=%s)", sessionId, sessionkey) go_lib.LogWarnln(warningMsg) return nil, nil } grantors, err := dao.GetHash(sessionkey, SESSION_GRANTORS_KEY) if err != nil { return nil, err } if len(grantors) == 0 { warningMsg := fmt.Sprintf("Not found grantor from session (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_GRANTORS_KEY) go_lib.LogWarnln(warningMsg) return nil, nil } groupName, err := dao.GetHash(sessionkey, SESSION_GROUP_KEY) if err != nil { return nil, err } if len(groupName) == 0 { warningMsg := fmt.Sprintf("Not found group name from session (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_GROUP_KEY) go_lib.LogWarnln(warningMsg) return nil, err } servivalSecondsLiterals, err := dao.GetHash(sessionkey, SESSION_SURVIVAL_SECONDS_KEY) if err != nil { return nil, err } if len(servivalSecondsLiterals) == 0 { warningMsg := fmt.Sprintf("Not found session servival seconds. Use default value '0'. (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_SURVIVAL_SECONDS_KEY) go_lib.LogWarnln(warningMsg) return nil, err } hmSession := &MySession{key: sessionkey, sessionId: sessionId, w: w, r: r} return hmSession, nil }
func (self *MySession) Get(name string) (string, error) { if len(name) == 0 { errorMsg := fmt.Sprintln("The parameter named name is EMPTY!") return "", errors.New(errorMsg) } value, err := dao.GetHash(self.key, name) if err != nil { return "", err } return value, nil }
func ClearPageAccessRecord(pageName string, visitor string) (bool, error) { if len(pageName) == 0 { return false, errors.New("The parameter named pageName is EMPTY!") } sign := getSignForPage(pageName) sign.Set() defer sign.Unset() parameterInfo := fmt.Sprintf("(pageName=%s, visitor=%s)", pageName, visitor) var result bool conn := dao.RedisPool.Get() defer conn.Close() value, err := dao.GetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName) if err != nil { return false, err } visitorAccessRecords, err := parseVisitorAccessRecords(value) if err != nil { base.Logger().Errorf("Parsing visitor access records error: %s %s\n", err, parameterInfo) } if visitorAccessRecords != nil { _, ok := visitorAccessRecords[visitor] if ok { delete(visitorAccessRecords, visitor) literals, err := formatVisitorAccessRecords(visitorAccessRecords) if err != nil { base.Logger().Errorf("Formating visitor access records error: %s %s\n", err, parameterInfo) } else { result, err = dao.SetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName, literals) if err != nil { return false, err } } } } if result { base.Logger().Infof("The page access info has been cleared. %s\n", parameterInfo) } else { base.Logger().Warnf("The page access info failed to clear. %s\n", parameterInfo) } return result, nil }
func GetUserGroup(groupName string) (*UserGroup, error) { if len(groupName) == 0 { debug.PrintStack() return nil, errors.New("The parameter named groupName is EMPTY!") } groupRightsLiterals, err := dao.GetHash(dao.USER_GROUP_KEY, groupName) if err != nil { return nil, err } if len(groupRightsLiterals) == 0 { return nil, nil } groupRights, err := UnmarshalGroupRights(groupRightsLiterals) if err != nil { return nil, err } userGroup := new(UserGroup) userGroup.Name = groupName userGroup.Rights = groupRights return userGroup, nil }
func AddPageAccessRecord(pageName string, visitor string, number uint64) (bool, error) { if len(pageName) == 0 { return false, errors.New("The parameter named pageName is EMPTY!") } sign := getSignForPage(pageName) sign.Set() defer sign.Unset() parameterInfo := fmt.Sprintf("(pageName=%s, visitor=%s, number=%d)", pageName, visitor, number) var result bool conn := dao.RedisPool.Get() defer conn.Close() value, err := dao.GetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName) if err != nil { return false, err } visitorAccessRecords, err := parseVisitorAccessRecords(value) if err != nil { go_lib.LogErrorf("Parsing visitor access records error: %s %s\n", err, parameterInfo) } if visitorAccessRecords != nil { visitorAccessRecords[visitor] = visitorAccessRecords[visitor] + uint64(number) literals, err := formatVisitorAccessRecords(visitorAccessRecords) if err != nil { go_lib.LogErrorf("Formating visitor access records error: %s %s\n", err, parameterInfo) } else { result, err = dao.SetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName, literals) if err != nil { return result, err } } } if result { go_lib.LogInfof("The page access info has been recorded. %s\n", parameterInfo) } else { go_lib.LogWarnf("The page access info failed to record. %s\n", parameterInfo) } return result, nil }
func GetPageAccessRecords(pageName string) (map[string]uint64, error) { if len(pageName) == 0 { return nil, errors.New("The parameter named pageName is EMPTY!") } sign := getSignForPage(pageName) sign.Set() defer sign.Unset() parameterInfo := fmt.Sprintf("(pageName=%s)", pageName) conn := dao.RedisPool.Get() defer conn.Close() value, err := dao.GetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName) if err != nil { errorMsg := fmt.Sprintf("Getting visitor access records error: %s %s\n", err, parameterInfo) return nil, errors.New(errorMsg) } var visitorAccessRecords map[string]uint64 visitorAccessRecords, err = parseVisitorAccessRecords(value) if err != nil { errorMsg := fmt.Sprintf("Parsing visitor access records error: %s %s\n", err, parameterInfo) return nil, errors.New(errorMsg) } return visitorAccessRecords, nil }