Esempio n. 1
0
func (db *mongoDatabase) fetchTeams(perm *MongoPerm) (map[bson.ObjectId]*models.Group, error) {
	if perm.TeamModel != nil {
		return map[bson.ObjectId]*models.Group{
			perm.TeamModel.Id: perm.TeamModel,
		}, nil
	}

	belongs := modelhelper.Selector{
		"targetId":   perm.AccModel.Id,
		"sourceName": "JGroup",
		"as":         "member",
	}

	rels, err := modelhelper.GetAllRelationships(belongs)
	if err != nil {
		return nil, models.ResError(err, "jRelationship")
	}

	ids := make([]bson.ObjectId, len(rels))

	for i := range rels {
		ids[i] = rels[i].SourceId
	}

	groups, err := modelhelper.GetGroupsByIds(ids...)
	if err != nil {
		return nil, models.ResError(err, "jGroup")
	}

	teams := make(map[bson.ObjectId]*models.Group, len(groups))

	for i := range groups {
		teams[groups[i].Id] = groups[i]
	}

	return teams, nil
}
Esempio n. 2
0
func (db *mongoDatabase) fetchCreds(f *Filter, acc *models.Account, user *models.User,
	teams map[bson.ObjectId]*models.Group, belongs modelhelper.Selector, creds *[]*Cred) error {

	db.log().Debug("fetching credentials for %+v", belongs)

	rels, err := modelhelper.GetAllRelationships(belongs)
	if err != nil && err != mgo.ErrNotFound {
		return models.ResError(err, "jRelationship")
	}

	ids := make([]bson.ObjectId, len(rels))

	for i := range rels {
		ids[i] = rels[i].TargetId
	}

	c, err := modelhelper.GetCredentialByIDs(ids...)
	if err == mgo.ErrNotFound {
		return nil // nothing to fetch, ignore dangling jRelationship
	}
	if err != nil {
		return err
	}

	credentials := make(map[bson.ObjectId]*models.Credential, len(c))

	for i := range c {
		credentials[c[i].Id] = c[i]
	}

	for _, rel := range rels {
		cred, ok := credentials[rel.TargetId]
		if !ok {
			continue // ignore dangling jRelationship
		}

		if f.Provider != "" && cred.Provider != f.Provider {
			// provider does not contains, filter out
			continue
		}

		c := &Cred{
			Ident:    cred.Identifier,
			Provider: cred.Provider,
			Title:    cred.Title,
			Perm: &MongoPerm{
				AccModel:  acc,
				UserModel: user,
				CredModel: cred,
				RoleNames: UserRole,
			},
		}

		db.log().Debug("fetched %+v", c)

		if team, ok := teams[rel.SourceId]; ok {
			c.Team = team.Slug
			c.Perm.(*MongoPerm).TeamModel = team
		}

		*creds = append(*creds, c)
	}

	return nil
}