Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to modify a user #434

Merged
merged 7 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/content/resources/user.group.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "user.group"
slug: "user-group"
date: "2016-10-04T13:01:50-05:00"
date: "2016-10-28T08:47:08-05:00"
menu:
main:
parent: resources
Expand Down Expand Up @@ -43,5 +43,6 @@ The group Name will be changed to NewName.
Valid values: `present` and `absent`

State is whether the group should be present.
The default value is present.


35 changes: 32 additions & 3 deletions docs/content/resources/user.user.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "user.user"
slug: "user-user"
date: "2016-10-04T13:01:50-05:00"
date: "2016-10-28T08:47:08-05:00"
menu:
main:
parent: resources
Expand All @@ -28,6 +28,13 @@ user.user "user" {

Username is the user login name.

- `new_username` (string)

NewUsername is used when modifying a user.
Username will be changed to NewUsername. No changes to the home directory
name or location of the contents will be made. This can be done using
HomeDir and MoveDir options.

- `uid` (optional uint32)

UID is the user ID.
Expand All @@ -51,17 +58,39 @@ Only one of GID or Groupname may be indicated.
- `name` (string)

Name is the user description.
This field can be indicated when adding or modifying a user.

- `create_home` (bool)

CreateHome when set to true will create the home directory for the user.
The files and directories contained in the skeleton directory (which can be
defined with the SkelDir option) will be copied to the home directory.

- `skel_dir` (string)

SkelDir contains files and directories to be copied in the user's home
directory when adding a user. If not set, the skeleton directory is defined
by the SKEL variable in /etc/default/useradd or, by default, /etc/skel.
SkelDir is only valid is CreatHome is specified.

- `home_dir` (string)

HomeDir is the user's login directory. By default, the login
name is appended to the home directory.
HomeDir is the name of the user's login directory. If not set, the home
directory is defined by appending the value of Username to the HOME
variable in /etc/default/useradd, resulting in /HOME/Username.
This field can be indicated when adding or modifying a user.

- `move_dir` (bool)

MoveDir is used to move the contents of HomeDir when modifying a user.
HomeDir must also be indicated if MoveDir is set to true.

- `state` (State)


Valid values: `present` and `absent`

State is whether the user should be present.
The default value is present.


1 change: 1 addition & 0 deletions resource/group/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Preparer struct {
NewName string `hcl:"new_name"`

// State is whether the group should be present.
// The default value is present.
State State `hcl:"state" valid_values:"present,absent"`
}

Expand Down
41 changes: 39 additions & 2 deletions resource/user/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type Preparer struct {
// Username is the user login name.
Username string `hcl:"username" required:"true"`

// NewUsername is used when modifying a user.
// Username will be changed to NewUsername. No changes to the home directory
// name or location of the contents will be made. This can be done using
// HomeDir and MoveDir options.
NewUsername string `hcl:"new_username"`

// UID is the user ID.
UID *uint32 `hcl:"uid"`

Expand All @@ -41,13 +47,32 @@ type Preparer struct {
GID *uint32 `hcl:"gid" mutually_exclusive:"gid,groupname"`

// Name is the user description.
// This field can be indicated when adding or modifying a user.
Name string `hcl:"name"`

// HomeDir is the user's login directory. By default, the login
// name is appended to the home directory.
// CreateHome when set to true will create the home directory for the user.
// The files and directories contained in the skeleton directory (which can be
// defined with the SkelDir option) will be copied to the home directory.
CreateHome bool `hcl:"create_home"`

// SkelDir contains files and directories to be copied in the user's home
// directory when adding a user. If not set, the skeleton directory is defined
// by the SKEL variable in /etc/default/useradd or, by default, /etc/skel.
// SkelDir is only valid is CreatHome is specified.
SkelDir string `hcl:"skel_dir"`

// HomeDir is the name of the user's login directory. If not set, the home
// directory is defined by appending the value of Username to the HOME
// variable in /etc/default/useradd, resulting in /HOME/Username.
// This field can be indicated when adding or modifying a user.
HomeDir string `hcl:"home_dir"`

// MoveDir is used to move the contents of HomeDir when modifying a user.
// HomeDir must also be indicated if MoveDir is set to true.
MoveDir bool `hcl:"move_dir"`

// State is whether the user should be present.
// The default value is present.
State State `hcl:"state" valid_values:"present,absent"`
}

Expand All @@ -63,15 +88,27 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) {
return nil, fmt.Errorf("user \"gid\" parameter out of range")
}

if p.SkelDir != "" && !p.CreateHome {
return nil, fmt.Errorf("user \"create_home\" parameter required with \"skel_dir\" parameter")
}

if p.MoveDir && p.HomeDir == "" {
return nil, fmt.Errorf("user \"home_dir\" parameter required with \"move_dir\" parameter")
}

if p.State == "" {
p.State = StatePresent
}

usr := NewUser(new(System))
usr.Username = p.Username
usr.NewUsername = p.NewUsername
usr.GroupName = p.GroupName
usr.Name = p.Name
usr.CreateHome = p.CreateHome
usr.SkelDir = p.SkelDir
usr.HomeDir = p.HomeDir
usr.MoveDir = p.MoveDir
usr.State = p.State

if p.UID != nil {
Expand Down
35 changes: 35 additions & 0 deletions resource/user/preparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ func TestPrepare(t *testing.T) {
assert.NoError(t, err)
})

t.Run("create_home and skel_dir parameters", func(t *testing.T) {
p := user.Preparer{UID: &testID, GID: &testID, Username: "test", CreateHome: true, SkelDir: "/etc/skel", HomeDir: "tmp", State: user.StateAbsent}
_, err := p.Prepare(&fr)

assert.NoError(t, err)
})

t.Run("create_home parameter", func(t *testing.T) {
p := user.Preparer{UID: &testID, GID: &testID, Username: "test", CreateHome: true, HomeDir: "tmp", State: user.StateAbsent}
_, err := p.Prepare(&fr)

assert.NoError(t, err)
})

t.Run("no name parameter", func(t *testing.T) {
p := user.Preparer{UID: &testID, GID: &testID, Username: "test", HomeDir: "tmp", State: user.StateAbsent}
_, err := p.Prepare(&fr)
Expand All @@ -119,6 +133,13 @@ func TestPrepare(t *testing.T) {

assert.NoError(t, err)
})

t.Run("home_dir and move_dir parameters", func(t *testing.T) {
p := user.Preparer{Username: "test", MoveDir: true, HomeDir: "tmp"}
_, err := p.Prepare(&fr)

assert.NoError(t, err)
})
})

t.Run("invalid", func(t *testing.T) {
Expand All @@ -135,5 +156,19 @@ func TestPrepare(t *testing.T) {

assert.EqualError(t, err, fmt.Sprintf("user \"gid\" parameter out of range"))
})

t.Run("no create_home with skel_dir", func(t *testing.T) {
p := user.Preparer{UID: &testID, GID: &testID, Username: "test", SkelDir: "/etc/skel", HomeDir: "tmp", State: user.StateAbsent}
_, err := p.Prepare(&fr)

assert.EqualError(t, err, fmt.Sprintf("user \"create_home\" parameter required with \"skel_dir\" parameter"))
})

t.Run("no home_dir with move_dir", func(t *testing.T) {
p := user.Preparer{Username: "test", MoveDir: true}
_, err := p.Prepare(&fr)

assert.EqualError(t, err, fmt.Sprintf("user \"home_dir\" parameter required with \"move_dir\" parameter"))
})
})
}
Loading