// FindOrganizations returns a list of organizations that match filter and the total count of matching organizations.
422859
func (s *Service) FindOrganizations(ctx context.Context, filter platform.OrganizationFilter, opt ...platform.FindOptions) ([]*platform.Organization, int, error) {
	op := OpPrefix + platform.OpFindOrganizations
	if filter.ID != nil {
		o, err := s.FindOrganizationByID(ctx, *filter.ID)
		if err != nil {
			return nil, 0, &platform.Error{
				Op:  op,
				Err: err,
			}
		}

		return []*platform.Organization{o}, 1, nil
	}

	filterFunc := func(o *platform.Organization) bool { return true }
	if filter.Name != nil {
		filterFunc = func(o *platform.Organization) bool {
			return o.Name == *filter.Name
		}
	}

	orgs, pe := s.filterOrganizations(ctx, filterFunc)
	if pe != nil {
		return nil, 0, &platform.Error{
			Err: pe,
			Op:  op,
		}
	}

	if len(orgs) == 0 {
		return orgs, 0, &platform.Error{
			Code: platform.ENotFound,
			Op:   op,
			Msg:  errOrganizationNotFound,
		}
	}

	return orgs, len(orgs), nil
}
1145852
func (s *OrganizationService) UpdateOrganization(ctx context.Context, id platform.ID, upd platform.OrganizationUpdate) (*platform.Organization, error) {
	return s.UpdateOrganizationF(ctx, id, upd)
}
682219
func (s *Service) RemoveOrganization(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	id := httprouter.GetParamFromContext(ctx, "oid")

	org, err := s.Store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &id})
	if err != nil {
		Error(w, http.StatusNotFound, err.Error(), s.Logger)
		return
	}
	if err := s.Store.Organizations(ctx).Delete(ctx, org); err != nil {
		Error(w, http.StatusBadRequest, err.Error(), s.Logger)
		return
	}
	w.WriteHeader(http.StatusNoContent)
}
134364
func (c *Client) FindOrganization(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) {
	op := getOp(influxdb.OpFindOrganization)
	if filter.ID != nil {
		o, err := c.FindOrganizationByID(ctx, *filter.ID)
		if err != nil {
			return nil, &influxdb.Error{
				Err: err,
				Op:  op,
			}
		}
		return o, nil
	}

	if filter.Name != nil {
		o, err := c.FindOrganizationByName(ctx, *filter.Name)
		if err != nil {
			return nil, &influxdb.Error{
				Err: err,
				Op:  op,
			}
		}
		return o, nil
	}

	// If name and ID are not set, then, this is an invalid usage of the API.
	return nil, &influxdb.Error{
		Code: influxdb.EInvalid,
		Msg:  "no filter parameters provided",
	}
}
411693
func (s *Service) FindOrganization(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) {
	if filter.ID != nil {
		return s.FindOrganizationByID(ctx, *filter.ID)
	}

	if filter.Name != nil {
		return s.FindOrganizationByName(ctx, *filter.Name)
	}

	// If name and ID are not set, then, this is an invalid usage of the API.
	return nil, &influxdb.Error{
		Code: influxdb.EInvalid,
		Msg:  "no filter parameters provided",
	}
}
118740
func (c *Client) FindOrganization(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) {
	op := getOp(platform.OpFindOrganization)
	if filter.ID != nil {
		o, err := c.FindOrganizationByID(ctx, *filter.ID)
		if err != nil {
			return nil, &platform.Error{
				Err: err,
				Op:  op,
			}
		}
		return o, nil
	}

	if filter.Name != nil {
		o, err := c.FindOrganizationByName(ctx, *filter.Name)
		if err != nil {
			return nil, &platform.Error{
				Err: err,
				Op:  op,
			}
		}
		return o, nil
	}

	filterFn := filterOrganizationsFn(filter)

	var o *platform.Organization
	err := c.db.View(func(tx *bolt.Tx) error {
		return forEachOrganization(ctx, tx, func(org *platform.Organization) bool {
			if filterFn(org) {
				o = org
				return false
			}
			return true
		})
	})

	if err != nil {
		return nil, &platform.Error{
			Op:  op,
			Err: err,
		}
	}

	if o == nil {
		return nil, &platform.Error{
			Code: platform.ENotFound,
			Op:   op,
			Msg:  "organization not found",
		}
	}

	return o, nil
}
1268363
func (api OrganizationsAPI) GetOrganizationUsers(w http.ResponseWriter, r *http.Request) {
	globalID := mux.Vars(r)["globalid"]
	orgMgr := organization.NewManager(r)
	if !orgMgr.Exists(globalID) {
		writeErrorResponse(w, http.StatusNotFound, "organization_not_found")
		return
	}
	authenticatedUser := context.Get(r, "authenticateduser").(string)
	response := organization.GetOrganizationUsersResponseBody{}
	isOwner, err := orgMgr.IsOwner(globalID, authenticatedUser)
	if handleServerError(w, "checking if user is owner of an organization", err) {
		return
	}
	org, err := orgMgr.GetByName(globalID)
	if handleServerError(w, "getting organization by name", err) {
		return
	}
	roleMap := make(map[string]string)
	valMgr := validationdb.NewManager(r)
	allUsernames := append(org.Members, org.Owners...)
	userIdentifierMap, err := organization.MapUsernamesToIdentifiers(allUsernames, valMgr)
	if handleServerError(w, "mapping usernames to identifiers", err) {
		return
	}
	for _, username := range org.Members {
		roleMap[username] = "members"
	}
	for _, username := range org.Owners {
		roleMap[username] = "owners"
	}
	authorizationsMap := make(map[string]user.Authorization)
	// Only owners can see if there are missing permissions
	userMgr := user.NewManager(r)
	if isOwner {
		authorizations, err := userMgr.GetOrganizationAuthorizations(globalID)
		if handleServerError(w, "getting organizaton authorizations", err) {
			return
		}
		for _, authorization := range authorizations {
			authorizationsMap[authorization.Username] = authorization
		}
	}
	users := []organization.OrganizationUser{}
	for username, role := range roleMap {
		orgUser := organization.OrganizationUser{
			Username:      getUserIdentifier(username, userIdentifierMap),
			Role:          role,
			MissingScopes: []string{},
		}
		if isOwner {
			for _, requiredScope := range org.RequiredScopes {
				hasScope := false
				if authorization, hasKey := authorizationsMap[username]; hasKey {
					hasScope = requiredScope.IsAuthorized(authorization)
				} else {
					hasScope = false
				}
				if !hasScope {
					orgUser.MissingScopes = append(orgUser.MissingScopes, requiredScope.Scope)
				}
			}
		}
		users = append(users, orgUser)
	}
	response.HasEditPermissions = isOwner
	response.Users = users
	json.NewEncoder(w).Encode(response)
}
1030571
func UsingOrganization(orgID string) Option {
	return func(api *API) error {
		api.OrganizationID = orgID
		return nil
	}
}
463004
func (p *PushEventRepository) GetOrganization() string {
	if p == nil || p.Organization == nil {
		return ""
	}
	return *p.Organization
}
61152
func (c *Contributor) GetOrganizationsURL() string {
	if c == nil || c.OrganizationsURL == nil {
		return ""
	}
	return *c.OrganizationsURL
}
939049
func (m *Manager) CountByOrganization(organization string) (int, error) {
	qry := bson.M{"orgowners": organization}
	return m.collection.Find(qry).Count()
}
508548
func (m *Manager) GetSubOrganizations(globalID string) ([]Organization, error) {
	var organizations = make([]Organization, 0, 0)
	var qry = bson.M{"globalid": bson.M{"$regex": bson.RegEx{"^" + globalID + `\.`, ""}}}
	if err := m.collection.Find(qry).All(&organizations); err != nil {
		return nil, err
	}

	return organizations, nil
}
1265917
func NewOrganizations(dir string, logger chronograf.Logger) chronograf.OrganizationsStore {
	return &Organizations{
		Dir:     dir,
		Load:    load,
		ReadDir: ioutil.ReadDir,
		Logger:  logger,
	}
}
167681
func (s *Store) Organizations(ctx context.Context) chronograf.OrganizationsStore {
	if isServer := hasServerContext(ctx); isServer {
		return s.OrganizationsStore
	}
	if isSuperAdmin := hasSuperAdminContext(ctx); isSuperAdmin {
		return s.OrganizationsStore
	}
	if org, ok := hasOrganizationContext(ctx); ok {
		return organizations.NewOrganizationsStore(s.OrganizationsStore, org)
	}
	return &noop.OrganizationsStore{}
}
444577
func (s *OrganizationService) FindOrganizations(ctx context.Context, filter influxdb.OrganizationFilter, opt ...influxdb.FindOptions) ([]*influxdb.Organization, int, error) {
	span, _ := tracing.StartSpanFromContext(ctx)
	defer span.Finish()

	if filter.Name != nil {
		span.LogKV("org", *filter.Name)
	}
	if filter.ID != nil {
		span.LogKV("org-id", *filter.ID)
	}
	for _, o := range opt {
		if o.Offset != 0 {
			span.LogKV("offset", o.Offset)
		}
		span.LogKV("descending", o.Descending)
		if o.Limit > 0 {
			span.LogKV("limit", o.Limit)
		}
		if o.SortBy != "" {
			span.LogKV("sortBy", o.SortBy)
		}
	}

	url, err := newURL(s.Addr, organizationPath)
	if err != nil {
		return nil, 0, tracing.LogError(span, err)
	}
	qp := url.Query()

	if filter.Name != nil {
		qp.Add(OrgName, *filter.Name)
	}
	if filter.ID != nil {
		qp.Add(OrgID, filter.ID.String())
	}
	url.RawQuery = qp.Encode()

	req, err := http.NewRequest("GET", url.String(), nil)
	if err != nil {
		return nil, 0, tracing.LogError(span, err)
	}
	tracing.InjectToHTTPRequest(span, req)

	SetToken(s.Token, req)
	hc := newClient(url.Scheme, s.InsecureSkipVerify)

	resp, err := hc.Do(req)
	if err != nil {
		return nil, 0, tracing.LogError(span, err)
	}
	defer resp.Body.Close()

	if err := CheckError(resp); err != nil {
		return nil, 0, tracing.LogError(span, err)
	}

	var os orgsResponse
	if err := json.NewDecoder(resp.Body).Decode(&os); err != nil {
		return nil, 0, tracing.LogError(span, err)
	}

	orgs := os.Toinfluxdb()
	return orgs, len(orgs), nil
}
50762
func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
	request, err := client.newHTTPRequest(requestOptions{
		RequestName: internal.GetOrganizationRequest,
		URIParams:   Params{"organization_guid": guid},
	})
	if err != nil {
		return Organization{}, nil, err
	}

	var org Organization
	response := cloudcontroller.Response{
		DecodeJSONResponseInto: &org,
	}

	err = client.connection.Make(request, &response)
	return org, response.Warnings, err
}
1014832
func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error) {
	return s.getInstallation(ctx, fmt.Sprintf("orgs/%v/installation", org))
}