Skip to content

Commit

Permalink
[FABC-829] Add hf.AffiliationMgr and hf.GenCRL attributes to migrated (
Browse files Browse the repository at this point in the history
…#159)

registrar users

Signed-off-by: Danny Cao <dcao@us.ibm.com>
(cherry picked from commit 5180751)
  • Loading branch information
caod123 authored and mastersingh24 committed Jun 24, 2020
1 parent f9a3427 commit fcda8bb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
44 changes: 44 additions & 0 deletions lib/server/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,14 @@ func (u *Impl) Migrate(tx userDB) error {
}
currentLevel++
}

if currentLevel < 2 {
err := u.migrateUserToLevel2(tx)
if err != nil {
return err
}
currentLevel++
}
return nil
}

Expand Down Expand Up @@ -548,6 +556,42 @@ func (u *Impl) migrateUserToLevel1(tx userDB) error {
return nil
}

func (u *Impl) migrateUserToLevel2(tx userDB) error {
log.Debugf("Migrating user '%s' to level 2", u.GetName())

// Update identity to level 2
// Only give these attributes to a registrar user
_, err := u.GetAttribute("hf.Registrar.Roles") // Check if user is a registrar
if err == nil {
_, err := u.GetAttribute("hf.AffiliationMgr") // Check if user already has "hf.AffiliationMgr" attribute
if err != nil {
newAttr := api.Attribute{Name: "hf.AffiliationMgr", Value: "true"}
err := u.ModifyAttributesTx(tx, []api.Attribute{newAttr})
if err != nil {
return errors.WithMessage(err, "Failed to set attribute")
}
u.attrs[newAttr.Name] = newAttr
}

_, err = u.GetAttribute("hf.GenCRL") // Check if user already has "hf.GenCRL" attribute
if err != nil {
newAttr := api.Attribute{Name: "hf.GenCRL", Value: "true"}
err := u.ModifyAttributesTx(tx, []api.Attribute{newAttr})
if err != nil {
return errors.WithMessage(err, "Failed to set attribute")
}
u.attrs[newAttr.Name] = newAttr
}
}

err = u.setLevel(tx, 2)
if err != nil {
return errors.WithMessage(err, "Failed to update level of user")
}

return nil
}

// Affilation is interface that defines functions needed to get a user's affiliation
type Affilation interface {
GetAffiliationPath() []string
Expand Down
20 changes: 17 additions & 3 deletions lib/server/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,27 @@ var _ = Describe("user", func() {
})

Context("migrate", func() {
It("adds new attribute to user", func() {
It("adds new attributes to user", func() {
_, err := u.GetAttribute("hf.Registrar.Attributes")
Expect(err).To(HaveOccurred())
_, err = u.GetAttribute("hf.AffiliationMgr")
Expect(err).To(HaveOccurred())
_, err = u.GetAttribute("hf.GenCRL")
Expect(err).To(HaveOccurred())

mockResult.RowsAffectedReturns(int64(1), nil)
mockUserDB.ExecReturns(mockResult, nil)
err := u.Migrate(mockUserDB)
err = u.Migrate(mockUserDB)
Expect(err).NotTo(HaveOccurred())
val, err := u.GetAttribute("hf.Registrar.Attributes")
Expect(err).NotTo(HaveOccurred())
Expect(val.Value).To(Equal("*"))
val, err = u.GetAttribute("hf.AffiliationMgr")
Expect(err).NotTo(HaveOccurred())
_, err = u.GetAttribute("hf.Registrar.Attributes")
Expect(val.Value).To(Equal("true"))
val, err = u.GetAttribute("hf.GenCRL")
Expect(err).NotTo(HaveOccurred())
Expect(val.Value).To(Equal("true"))
})
})

Expand Down

0 comments on commit fcda8bb

Please sign in to comment.