fix: handle domain claimed event correctly for service users (#1711)

* fix: handle domain claimed event correctly on user view

* fix: ignore domain claimed events for email notifications

* fix: change org

* handle org changed in read models correctly

* fix: change org in user grant handler

Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
This commit is contained in:
Livio Amstutz 2021-05-04 14:57:11 +02:00 committed by GitHub
parent 87c1dfa3aa
commit bc633627c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 60 additions and 24 deletions

View File

@ -74,11 +74,7 @@ func (o *Org) Reduce(event *es_models.Event) error {
return err
}
case model.OrgChanged:
err := org.SetData(event)
if err != nil {
return err
}
org, err = o.view.OrgByID(org.ID)
org, err := o.view.OrgByID(event.ResourceOwner)
if err != nil {
return err
}

View File

@ -69,7 +69,7 @@ func (s *Server) AddOrg(ctx context.Context, req *mgmt_pb.AddOrgRequest) (*mgmt_
func (s *Server) UpdateOrg(ctx context.Context, req *mgmt_pb.UpdateOrgRequest) (*mgmt_pb.UpdateOrgResponse, error) {
ctxData := authz.GetCtxData(ctx)
org, err := s.command.ChangeOrg(ctx, ctxData.ResourceOwner, req.Name)
org, err := s.command.ChangeOrg(ctx, ctxData.OrgID, req.Name)
if err != nil {
return nil, err
}

View File

@ -71,11 +71,7 @@ func (o *Org) Reduce(event *es_models.Event) (err error) {
case model.OrgAdded:
err = org.AppendEvent(event)
case model.OrgChanged:
err = org.SetData(event)
if err != nil {
return err
}
org, err = o.view.OrgByID(org.ID)
org, err = o.view.OrgByID(event.ResourceOwner)
if err != nil {
return err
}

View File

@ -204,6 +204,22 @@ func (u *UserGrant) processOrg(event *es_models.Event) (err error) {
member := new(org_es_model.OrgMember)
member.SetData(event)
return u.processMember(event, "ORG", "", member.UserID, member.Roles)
case org_es_model.OrgChanged:
grants, err := u.view.UserGrantsByProjectID(event.AggregateID)
if err != nil {
return err
}
if len(grants) == 0 {
return u.view.ProcessedUserGrantSequence(event)
}
org, err := u.getOrgByID(context.Background(), event.AggregateID)
if err != nil {
return err
}
for _, grant := range grants {
u.fillOrgData(grant, org)
}
return u.view.PutUserGrants(grants, event)
default:
return u.view.ProcessedUserGrantSequence(event)
}

View File

@ -29,6 +29,10 @@ func (v *View) UserGrantsByProjectID(projectID string) ([]*model.UserGrantView,
return view.UserGrantsByProjectID(v.Db, userGrantTable, projectID)
}
func (v *View) UserGrantsByOrgID(projectID string) ([]*model.UserGrantView, error) {
return view.UserGrantsByOrgID(v.Db, userGrantTable, projectID)
}
func (v *View) UserGrantsByProjectAndUserID(projectID, userID string) ([]*model.UserGrantView, error) {
return view.UserGrantsByProjectAndUserID(v.Db, userGrantTable, projectID, userID)
}

View File

@ -74,11 +74,7 @@ func (o *Org) Reduce(event *es_models.Event) error {
return err
}
case model.OrgChanged:
err := org.SetData(event)
if err != nil {
return err
}
org, err = o.view.OrgByID(org.ID)
org, err := o.view.OrgByID(event.ResourceOwner)
if err != nil {
return err
}

View File

@ -71,11 +71,7 @@ func (o *Org) Reduce(event *es_models.Event) (err error) {
case model.OrgAdded:
err = org.AppendEvent(event)
case model.OrgChanged:
err = org.SetData(event)
if err != nil {
return err
}
org, err = o.view.OrgByID(org.ID)
org, err = o.view.OrgByID(event.ResourceOwner)
if err != nil {
return err
}

View File

@ -59,7 +59,7 @@ func (u *UserGrant) ViewModel() string {
}
func (_ *UserGrant) AggregateTypes() []es_models.AggregateType {
return []es_models.AggregateType{grant_es_model.UserGrantAggregate, usr_es_model.UserAggregate, proj_es_model.ProjectAggregate}
return []es_models.AggregateType{grant_es_model.UserGrantAggregate, usr_es_model.UserAggregate, proj_es_model.ProjectAggregate, org_es_model.OrgAggregate}
}
func (u *UserGrant) CurrentSequence() (uint64, error) {
@ -88,6 +88,8 @@ func (u *UserGrant) Reduce(event *es_models.Event) (err error) {
err = u.processUser(event)
case proj_es_model.ProjectAggregate:
err = u.processProject(event)
case org_es_model.OrgAggregate:
err = u.processOrg(event)
}
return err
}
@ -171,6 +173,29 @@ func (u *UserGrant) processProject(event *es_models.Event) (err error) {
}
}
func (u *UserGrant) processOrg(event *es_models.Event) (err error) {
switch event.Type {
case org_es_model.OrgChanged:
grants, err := u.view.UserGrantsByOrgID(event.AggregateID)
if err != nil {
return err
}
if len(grants) == 0 {
return u.view.ProcessedUserGrantSequence(event)
}
org, err := u.getOrgByID(context.Background(), event.AggregateID)
if err != nil {
return err
}
for _, grant := range grants {
u.fillOrgData(grant, org)
}
return u.view.PutUserGrants(grants, event)
default:
return u.view.ProcessedUserGrantSequence(event)
}
}
func (u *UserGrant) fillData(grant *view_model.UserGrantView, resourceOwner string) (err error) {
user, err := u.getUserByID(grant.UserID)
if err != nil {

View File

@ -284,6 +284,9 @@ func (n *Notification) handleDomainClaimed(event *models.Event) (err error) {
if err != nil {
return err
}
if user.LastEmail == "" {
return nil
}
colors, err := n.getLabelPolicy(context.Background())
if err != nil {
return err

View File

@ -274,10 +274,14 @@ func (u *UserView) AppendEvent(event *models.Event) (err error) {
es_model.MachineChanged:
err = u.setData(event)
case es_model.DomainClaimed:
u.UsernameChangeRequired = true
if u.HumanView != nil {
u.HumanView.UsernameChangeRequired = true
}
err = u.setData(event)
case es_model.UserUserNameChanged:
u.UsernameChangeRequired = false
if u.HumanView != nil {
u.HumanView.UsernameChangeRequired = false
}
err = u.setData(event)
case es_model.UserEmailChanged,
es_model.HumanEmailChanged: