Exemplo n.º 1
0
func (filter *MethodFilter) Execute(hbtt *habitat.Habitat, buff *buffer.Buffer) {
	if hbtt.Method != filter.Method {
		msg := fmt.Sprintf("This service only supports %s requests.", filter.Method)
		buff.StatusCode = http.StatusMethodNotAllowed
		buff.Error = errors.New(msg)
	}
}
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
}