marshalPrincipal outputs a PrincipalConfig in a format for REST API endpoints.
(database *db.Database, princ auth.Principal, includeDynamicGrantInfo bool)
| 1727 | |
| 1728 | // marshalPrincipal outputs a PrincipalConfig in a format for REST API endpoints. |
| 1729 | func marshalPrincipal(database *db.Database, princ auth.Principal, includeDynamicGrantInfo bool) (*auth.PrincipalConfig, error) { |
| 1730 | name := externalUserName(princ.Name()) |
| 1731 | info := auth.PrincipalConfig{ |
| 1732 | Name: &name, |
| 1733 | ExplicitChannels: princ.ExplicitChannels().AsSet(), |
| 1734 | } |
| 1735 | |
| 1736 | collectionAccess := princ.GetCollectionsAccess() |
| 1737 | if collectionAccess != nil && !database.OnlyDefaultCollection() { |
| 1738 | info.CollectionAccess = make(map[string]map[string]*auth.CollectionAccessConfig) |
| 1739 | for scopeName, scope := range collectionAccess { |
| 1740 | scopeAccessConfig := make(map[string]*auth.CollectionAccessConfig) |
| 1741 | for collectionName, collection := range scope { |
| 1742 | _, err := database.GetDatabaseCollection(scopeName, collectionName) |
| 1743 | // collection doesn't exist anymore, but did at some point |
| 1744 | if err != nil { |
| 1745 | continue |
| 1746 | } |
| 1747 | collectionAccessConfig := &auth.CollectionAccessConfig{ |
| 1748 | ExplicitChannels_: collection.ExplicitChannels().AsSet(), |
| 1749 | } |
| 1750 | if includeDynamicGrantInfo { |
| 1751 | if user, ok := princ.(auth.User); ok { |
| 1752 | channels, err := user.InheritedCollectionChannels(scopeName, collectionName) |
| 1753 | if err != nil { |
| 1754 | return nil, err |
| 1755 | } |
| 1756 | collectionAccessConfig.Channels_ = channels.AsSet() |
| 1757 | collectionAccessConfig.JWTChannels_ = user.CollectionJWTChannels(scopeName, collectionName).AsSet() |
| 1758 | lastUpdated := collection.JWTLastUpdated |
| 1759 | if lastUpdated != nil && !lastUpdated.IsZero() { |
| 1760 | collectionAccessConfig.JWTLastUpdated = lastUpdated |
| 1761 | } |
| 1762 | } else { |
| 1763 | collectionAccessConfig.Channels_ = princ.CollectionChannels(scopeName, collectionName).AsSet() |
| 1764 | } |
| 1765 | } |
| 1766 | scopeAccessConfig[collectionName] = collectionAccessConfig |
| 1767 | } |
| 1768 | info.CollectionAccess[scopeName] = scopeAccessConfig |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | if user, ok := princ.(auth.User); ok { |
| 1773 | email := user.Email() |
| 1774 | info.Email = &email |
| 1775 | info.Disabled = base.Ptr(user.Disabled()) |
| 1776 | info.ExplicitRoleNames = user.ExplicitRoles().AsSet() |
| 1777 | if includeDynamicGrantInfo { |
| 1778 | channels, err := user.InheritedCollectionChannels(base.DefaultScope, base.DefaultCollection) |
| 1779 | if err != nil { |
| 1780 | return nil, err |
| 1781 | } |
| 1782 | info.Channels = channels.AsSet() |
| 1783 | info.RoleNames = user.RoleNames().AllKeys() |
| 1784 | info.JWTIssuer = base.Ptr(user.JWTIssuer()) |
| 1785 | info.JWTRoles = user.JWTRoles().AsSet() |
| 1786 | info.JWTChannels = user.JWTChannels().AsSet() |
no test coverage detected