Example #1
0
func (self *MySession) Set(name string, value string) error {
	if len(name) == 0 {
		errorMsg := fmt.Sprintln("The parameter named name is EMPTY!")
		return errors.New(errorMsg)
	}
	_, err := dao.SetHash(self.key, name, value)
	return err
}
Example #2
0
func AddUserGroup(userGroup *UserGroup) error {
	if userGroup == nil || userGroup.Name == "" || len(userGroup.Rights.PageRights) == 0 {
		return errors.New("The parameter named userGroup is NOT Ready!")
	}
	groupRightsLiterals, err := MarshalGroupRights(userGroup.Rights)
	if err != nil {
		return err
	}
	conn := dao.RedisPool.Get()
	defer conn.Close()
	_, err = dao.SetHash(dao.USER_GROUP_KEY, userGroup.Name, groupRightsLiterals)
	if err != nil {
		return err
	}
	return nil
}
Example #3
0
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
}
Example #4
0
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
}
Example #5
0
func (self *MySession) Initialize(
	grantors string,
	// survivalSeconds:
	//  '<0'- Don't set/Delete cookie;
	//  '0' - Set temporary cookie;
	//  '>0'- Set long term cookie according to this value & Set session expires
	survivalSeconds int,
	w http.ResponseWriter,
	r *http.Request) error {
	if len(grantors) == 0 {
		errorMsg := fmt.Sprintln("The session grantors is EMPTY!")
		return errors.New(errorMsg)
	}
	if w == nil {
		errorMsg := fmt.Sprintln("The pointer of http response writer is NIL!")
		return errors.New(errorMsg)
	}
	if r == nil {
		errorMsg := fmt.Sprintln("The pointer of http request is NIL!")
		return errors.New(errorMsg)
	}
	self.w = w
	self.r = r
	self.sessionId = generateSessionId(grantors, r)
	self.key = generateSessionKey(self.sessionId)
	go_lib.LogInfof("Initialize session (key=%s, grantors=%s)...\n", self.key, grantors)
	exists, err := dao.Exists(self.key)
	if err != nil {
		return err
	}
	if exists {
		_, err = dao.DelKey(self.key)
		if err != nil {
			return err
		}
	}
	_, err = dao.SetHash(self.key, SESSION_GRANTORS_KEY, grantors)
	if err != nil {
		return err
	}
	_, err = dao.SetHash(self.key, SESSION_SURVIVAL_SECONDS_KEY, strconv.FormatInt(int64(survivalSeconds), 10))
	if err != nil {
		return err
	}
	cookieMaxAge := survivalSeconds
	if survivalSeconds <= 0 {
		cookieMaxAge = -1
	}
	go_lib.LogInfof("Set session cookie (value=%s, grantors=%s, maxAge=%d)...\n", self.sessionId, grantors, cookieMaxAge)
	result := hmSessionCookie.SetOne(self.w, SESSION_COOKIE_KEY, self.sessionId, cookieMaxAge)
	if result {
		go_lib.LogInfof("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is successful.\n", self.sessionId, grantors, cookieMaxAge)
	} else {
		go_lib.LogWarnf("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is failing!\n", self.sessionId, grantors, cookieMaxAge)
	}
	if survivalSeconds > 0 {
		done, err := dao.SetExpires(self.key, uint64(survivalSeconds))
		if err != nil || !done {
			warningMsg := fmt.Sprintf("Setting session expires failed! (key=%s, survivalSeconds=%d, error=%s)\n", self.key, survivalSeconds, err)
			go_lib.LogWarnln(warningMsg)
		}
	}
	_, err = dao.SetHash(SESSION_MAP_KEY, grantors, self.key)
	if err != nil {
		return err
	}
	return nil
}