Skip to content

Commit

Permalink
Merge branch 'clone-object-permissions'
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Mar 8, 2024
2 parents 5b90603 + d768b23 commit 185541e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
39 changes: 38 additions & 1 deletion cmd/profile/objectPermissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var (
objectName string
objectName string
sourceObject string
)

func init() {
Expand Down Expand Up @@ -52,6 +53,11 @@ func init() {
deleteObjectCmd.Flags().StringVarP(&objectName, "object", "o", "", "object name")
deleteObjectCmd.MarkFlagRequired("object")

cloneObjectCmd.Flags().StringVarP(&sourceObject, "source", "s", "", "source object name")
cloneObjectCmd.Flags().StringVarP(&objectName, "object", "o", "", "new object name")
cloneObjectCmd.MarkFlagRequired("source")
cloneObjectCmd.MarkFlagRequired("object")

showObjectCmd.Flags().StringVarP(&objectName, "object", "o", "", "object name")
showObjectCmd.MarkFlagRequired("object")

Expand Down Expand Up @@ -84,6 +90,7 @@ func init() {

ObjectPermissionsCmd.AddCommand(editObjectCmd)
ObjectPermissionsCmd.AddCommand(addObjectCmd)
ObjectPermissionsCmd.AddCommand(cloneObjectCmd)
ObjectPermissionsCmd.AddCommand(showObjectCmd)
ObjectPermissionsCmd.AddCommand(deleteObjectCmd)
ObjectPermissionsCmd.AddCommand(listObjectCmd)
Expand Down Expand Up @@ -132,6 +139,18 @@ var deleteObjectCmd = &cobra.Command{
},
}

var cloneObjectCmd = &cobra.Command{
Use: "clone -s SObject -o SObject [filename]...",
Short: "Clone object permissions",
Long: "Clone object permissions in profiles for a new object",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, file := range args {
cloneObject(file)
}
},
}

var showObjectCmd = &cobra.Command{
Use: "show -f Object [filename]...",
Short: "Show object permissions",
Expand Down Expand Up @@ -212,6 +231,24 @@ func updateObjectPermissions(file string, perms permissionset.ObjectPermissions)
}
}

func cloneObject(file string) {
p, err := profile.Open(file)
if err != nil {
log.Warn("parsing profile failed: " + err.Error())
return
}
err = p.CloneObjectPermissions(sourceObject, objectName)
if err != nil {
log.Warn(fmt.Sprintf("clone failed for %s: %s", file, err.Error()))
return
}
err = internal.WriteToFile(p, file)
if err != nil {
log.Warn("update failed: " + err.Error())
return
}
}

func addObjectPermissions(file string) {
p, err := profile.Open(file)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/force-md_profile_object-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Update object permissions

* [force-md profile](force-md_profile.md) - Manage Profiles
* [force-md profile object-permissions add](force-md_profile_object-permissions_add.md) - Add object permissions
* [force-md profile object-permissions clone](force-md_profile_object-permissions_clone.md) - Clone object permissions
* [force-md profile object-permissions delete](force-md_profile_object-permissions_delete.md) - Delete object permissions
* [force-md profile object-permissions edit](force-md_profile_object-permissions_edit.md) - Update object permissions
* [force-md profile object-permissions list](force-md_profile_object-permissions_list.md) - List object permissions
Expand Down
31 changes: 31 additions & 0 deletions docs/force-md_profile_object-permissions_clone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## force-md profile object-permissions clone

Clone object permissions

### Synopsis

Clone object permissions in profiles for a new object

```
force-md profile object-permissions clone -s SObject -o SObject [filename]... [flags]
```

### Options

```
-h, --help help for clone
-o, --object string new object name
-s, --source string source object name
```

### Options inherited from parent commands

```
--convert-xml-entities convert numeric xml entities to character entities (default true)
--silent show errors only
```

### SEE ALSO

* [force-md profile object-permissions](force-md_profile_object-permissions.md) - Update object permissions

29 changes: 29 additions & 0 deletions profile/objectPermissions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package profile

import (
"fmt"
"strings"

"github.com/imdario/mergo"
Expand Down Expand Up @@ -175,3 +176,31 @@ func (p *Profile) GetGrantedObjectPermissions() []permissionset.ObjectPermission
}
return objectPermissions
}

func (p *Profile) CloneObjectPermissions(src, dest string) error {
for _, f := range p.ObjectPermissions {
if strings.ToLower(f.Object) == strings.ToLower(dest) {
return fmt.Errorf("%s object already exists", dest)
}
}
found := false
for _, f := range p.ObjectPermissions {
if strings.ToLower(f.Object) == strings.ToLower(src) {
found = true
clone := permissionset.ObjectPermissions{}
clone.AllowCreate.Text = f.AllowCreate.Text
clone.AllowDelete.Text = f.AllowDelete.Text
clone.AllowEdit.Text = f.AllowEdit.Text
clone.AllowRead.Text = f.AllowRead.Text
clone.ModifyAllRecords.Text = f.ModifyAllRecords.Text
clone.ViewAllRecords.Text = f.ViewAllRecords.Text
clone.Object = dest
p.ObjectPermissions = append(p.ObjectPermissions, clone)
}
}
if !found {
return fmt.Errorf("source object %s not found", src)
}
p.ObjectPermissions.Tidy()
return nil
}

0 comments on commit 185541e

Please sign in to comment.