github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/registry/impl_invokecreateworkspaceid.go (about)

     1  /*
     2   * Copyright (c) 2022-present unTill Pro, Ltd.
     3   * @author Denis Gribanov
     4   */
     5  
     6  package registry
     7  
     8  import (
     9  	"fmt"
    10  	"hash/crc32"
    11  	"strconv"
    12  
    13  	"github.com/voedger/voedger/pkg/goutils/iterate"
    14  	"github.com/voedger/voedger/pkg/utils/federation"
    15  
    16  	"github.com/voedger/voedger/pkg/appdef"
    17  	"github.com/voedger/voedger/pkg/istructs"
    18  	"github.com/voedger/voedger/pkg/itokens"
    19  	"github.com/voedger/voedger/pkg/sys/authnz"
    20  	"github.com/voedger/voedger/pkg/sys/workspace"
    21  )
    22  
    23  // sys/registry app, triggered by cdoc.registry.Login
    24  // at pseudoWSID translated to AppWSID
    25  func invokeCreateWorkspaceIDProjector(federation federation.IFederation, tokensAPI itokens.ITokens) func(event istructs.IPLogEvent, s istructs.IState, intents istructs.IIntents) (err error) {
    26  	return func(event istructs.IPLogEvent, s istructs.IState, intents istructs.IIntents) (err error) {
    27  		return iterate.ForEachError(event.CUDs, func(rec istructs.ICUDRow) error {
    28  			if rec.QName() != QNameCDocLogin || !rec.IsNew() {
    29  				return nil
    30  			}
    31  			loginHash := rec.AsString(authnz.Field_LoginHash)
    32  			wsName := strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(loginHash))), decimalBase)
    33  			var wsKind appdef.QName
    34  			switch istructs.SubjectKindType(rec.AsInt32(authnz.Field_SubjectKind)) {
    35  			case istructs.SubjectKind_Device:
    36  				wsKind = authnz.QNameCDoc_WorkspaceKind_DeviceProfile
    37  			case istructs.SubjectKind_User:
    38  				wsKind = authnz.QNameCDoc_WorkspaceKind_UserProfile
    39  			default:
    40  				return fmt.Errorf("unsupported cdoc.registry.Login.subjectKind: %d", rec.AsInt32(authnz.Field_SubjectKind))
    41  			}
    42  			targetClusterID := istructs.ClusterID(rec.AsInt32(authnz.Field_ProfileCluster))
    43  			targetApp := rec.AsString(authnz.Field_AppName)
    44  			ownerWSID := event.Workspace()
    45  			wsidToCallCreateWSIDAt := istructs.NewWSID(targetClusterID, ownerWSID.BaseWSID())
    46  			templateName := ""
    47  			templateParams := ""
    48  			return workspace.ApplyInvokeCreateWorkspaceID(federation, s.App(), tokensAPI, wsName, wsKind, wsidToCallCreateWSIDAt,
    49  				targetApp, templateName, templateParams, rec, ownerWSID)
    50  		})
    51  	}
    52  }