Exemplo n.º 1
0
func (filter *TemplateFilter) Execute(hbtt *habitat.Habitat, buff *buffer.Buffer) {

	log.Println("Template execution by TemplateFilter.")

	var (
		err     error
		stuffer *taxidermist.Taxidermist = taxidermist.New()
	)

	log.Println("PATH:\t", filter.Path)
	if filter.Path == "" {
		err = stuffer.SetTemplatePathURL(hbtt.Request)
	} else {
		err = stuffer.SetTemplatePathManual(filter.Path)
	}
	if err != nil {
		buff.Set500(errors.Stack(err))
		return
	}

	{ // Insert user data into the taxidermist.
		usr_id, err := hbtt.GetUserKey()
		if err != nil {
			buff.Set500(errors.Stack(err))
			return
		}

		if usr_id != "" {
			usr, err := filter.UserGetter(usr_id)
			if err != nil {
				buff.Set500(errors.Stack(err))
				return
			}

			stuffer.UserName = usr.GetName()
			stuffer.LoggedIn = true
			stuffer.UserGroups = usr.GetGroups()
		}
	}

	stuffer.Content = buff.Object

	if err = stuffer.Present(buff); err != nil {
		buff.Set500(errors.Stack(err))
		return
	}
}
Exemplo n.º 2
0
func (filter *GroupFilter) Execute(hbtt *habitat.Habitat, buff *buffer.Buffer) {
	if len(filter.Groups) == 0 {
		return
	}

	userKey, err := hbtt.GetUserKey()
	if err != nil {
		buff.Set500(errors.Stack(err))
	}

	groupsUser, err := filter.GroupGetter(userKey)
	if err != nil {
		buff.Set500(errors.Stack(err))
	}

	if groupsUser == nil {
		buff.Error = errors.New("You are not logged in.")
		buff.StatusCode = http.StatusUnauthorized
		return
	}

	if len(groupsUser) == 0 {
		buff.Error = errors.New("The current user is unauthorized.")
		buff.StatusCode = http.StatusForbidden
		return
	}

	// If there is a match between the user's groups and the allowed groups, return.
	for _, gAllowed := range filter.Groups {
		for _, gUser := range groupsUser {
			if gAllowed == gUser {
				return
			}
		}
	}

	buff.Error = errors.New("The current user is unauthorized.")
	buff.StatusCode = http.StatusForbidden
}