Exemple #1
0
func TestValidSlice(t *testing.T) {
	letters := []string{"a", "b", "c", "d"}
	letters = utils.ReverseSlice(letters).([]string)
	if letters[len(letters)-1] != "a" {
		t.Errorf("Last letter should be 'a' but got: %v", letters[len(letters)-1])
	}

	if utils.InSlice("c", letters) == false {
		t.Errorf("Letter c is present in letters slice")
	}

	var horse, nope Amazing
	horse.Animal = "weebl"
	nope.Animal = "nerdz"

	complexData := []ComplexData{
		{One: 1, Two: "lol", Horse: horse},
		{One: 2, Two: "asd", Horse: nope}}

	complexData = utils.ReverseSlice(complexData).([]ComplexData)

	if complexData[0].Horse.Animal != "nerdz" {
		t.Errorf("Animal should be nerdz, but got: %v", complexData[0].Horse.Animal)
	}

	if utils.InSlice(complexData[1], complexData) == false {
		t.Errorf("This value is present in complexData slice")
	}

	if utils.InSlice("banana", complexData) {
		t.Errorf("Banana is not into complexData slice (and have different type)")
	}
}
Exemple #2
0
// CanSee returns true if the user can see the Board content
func (user *User) CanSee(board Board) bool {
	switch board.(type) {
	case *User:
		return !utils.InSlice(user.ID(), board.(*User).NumericBlacklist())

	case *Project:
		project := board.(*Project)
		if project.Visible {
			return true
		}

		return user.ID() == project.NumericOwner() || utils.InSlice(user.ID(), project.NumericMembers())
	}
	return false
}
// SetLanguage set the language of the comment (TODO: add db side column)
func (comment *UserPostComment) SetLanguage(language string) error {
	if utils.InSlice(language, Configuration.Languages) {
		//post.Lang = language
		return nil
	}
	return fmt.Errorf("Language '%s' is not a valid or supported language", language)
}
// SetLanguage set the language of the post
func (post *ProjectPost) SetLanguage(language string) error {
	if utils.InSlice(language, Configuration.Languages) {
		post.Lang = language
		return nil
	}
	return fmt.Errorf("Language '%s' is not a valid or supported language", language)
}
// SetLanguage set the language of the comment
func (comment *ProjectPostComment) SetLanguage(language string) error {
	if language == "" {
		language = comment.Sender().Language()
	}
	if utils.InSlice(language, Configuration.Languages) {
		comment.Lang = language
		return nil
	}
	return fmt.Errorf("Language '%s' is not a valid or supported language", language)
}
Exemple #6
0
// SetLanguage set the language of the post
func (post *UserPost) SetLanguage(language string) error {
	if language == "" {
		language = post.Sender().Language()
	}
	if utils.InSlice(language, Configuration.Languages) {
		post.Lang = language
		return nil
	}
	return fmt.Errorf("Language '%s' is not a valid or supported language", language)
}
Exemple #7
0
// isValidScope checks if scope is a valid scope
func (s *OAuth2Storage) isValidScope(scope string) error {
	scopes := strings.Split(scope, " ")
	for _, s := range scopes {
		parts := strings.Split(s, ":")
		if len(parts) != 2 {
			return errors.New("Scope (" + s + ") has invalid format. The valid format is: scope:[read,write]")
		}
		if !utils.InSlice(parts[0], Configuration.Scopes) {
			return errors.New("Requested scope (" + s + ") does not exist")
		}
		rw := strings.Split(parts[1], ",")
		for _, permission := range rw {
			if permission != "read" && permission != "write" {
				return errors.New("Invalid permission: " + permission + ". Allowed: read,write")
			}
		}
	}
	return nil
}
Exemple #8
0
// CanComment returns true if the user can comment to the existingPost
func (user *User) CanComment(message ExistingPost) bool {
	return !utils.InSlice(user.ID(), message.Sender().NumericBlacklist()) && message.ID() > 0 && !message.IsClosed()
}
Exemple #9
0
// CanLurk returns true if the user haven't lurked the existingPost yet
func (user *User) CanLurk(message ExistingPost) bool {
	return message.ID() > 0 && !utils.InSlice(user.ID(), message.NumericLurkers())
}
Exemple #10
0
// CanDelete returns true if user can delete the existingMessage
func (user *User) CanDelete(message existingMessage) bool {
	return message.ID() > 0 && utils.InSlice(user.ID(), message.NumericOwners())
}
Exemple #11
0
// CanEdit returns true if user can edit the editingMessage
func (user *User) CanEdit(message editingMessage) bool {
	return message.ID() > 0 && message.IsEditable() && utils.InSlice(user.ID(), message.NumericOwners())
}
Exemple #12
0
// canDelete returns trhe if user can delet ethe existingMessage
func (user *User) canDelete(message existingMessage) bool {
	return message.Id() > 0 && utils.InSlice(user.Counter, message.NumericOwners())
}
Exemple #13
0
// setPostlist is the middleware that sets "postlistOptions" = *nerdz.PostlistOptions into the current Context
// handle GET parameters:
// following: if setted, requires posts from following users
// followers: if setted, requires posts from followers users
// lang: if setted to a supported language (nerdz.Configuration.Languages), requires
//       posts in that language
// older: if setted to an existing hpid, requires posts older than the "older" value
// olderType: if setted can be only "user" or "project". Represents a reference to the older hpid type
//		used when fetching from a view, where hpid can be from posts or groups_posts
// newer: if setted to an existing hpid, requires posts newer than the "newer" value7
// newerType: if setted can be only "user" or "project". Represents a reference to the newer hpid type
//		used when fetching from a view, where hpid can be from posts or groups_posts
// n: if setted, define the number of posts to retrieve. Follows the nerdz.atMostPost rules
func setPostlist() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return echo.HandlerFunc(func(c echo.Context) error {
			var following, followers bool
			if c.QueryParam("following") != "" {
				following = true
			}

			if c.QueryParam("followers") != "" {
				followers = true
			}

			for _, t := range []string{"olderType", "newerType"} {
				tValue := c.QueryParam(t)
				if tValue != "" {
					if tValue != "user" && tValue != "project" {
						message := fmt.Sprintf(`Unsupported %s %s. Only "user" or "project" are allowed`, t, tValue)
						return c.JSON(http.StatusBadRequest, &rest.Response{
							HumanMessage: message,
							Message:      message,
							Status:       http.StatusBadRequest,
							Success:      false,
						})
					}
				}
			}

			var olderModel, newerModel igor.DBModel
			if c.QueryParam("olderType") == "user" {
				olderModel = nerdz.UserPost{}
			} else {
				olderModel = nerdz.ProjectPost{}
			}

			if c.QueryParam("newerType") == "user" {
				newerModel = nerdz.UserPost{}
			} else {
				newerModel = nerdz.ProjectPost{}
			}

			var language string
			lang := c.QueryParam("lang")
			if lang == "" {
				language = ""
			} else {
				if !utils.InSlice(lang, nerdz.Configuration.Languages) {
					message := "Not supported language: " + lang
					return c.JSON(http.StatusBadRequest, &rest.Response{
						HumanMessage: message,
						Message:      message,
						Status:       http.StatusBadRequest,
						Success:      false,
					})
				}
				language = lang
			}

			old := c.QueryParam("older")
			new := c.QueryParam("newer")

			older, _ := strconv.ParseUint(old, 10, 64)
			newer, _ := strconv.ParseUint(new, 10, 64)

			n, _ := strconv.ParseUint(c.QueryParam("n"), 10, 8)

			c.Set("postlistOptions", &nerdz.PostlistOptions{
				Following:  following,
				Followers:  followers,
				Language:   language,
				N:          nerdz.AtMostPosts(n),
				Older:      older,
				OlderModel: olderModel,
				Newer:      newer,
				NewerModel: newerModel,
			})

			return next(c)
		})
	}
}