Unverified Commit 65dc760f authored by Saturnino Abril's avatar Saturnino Abril Committed by GitHub
Browse files

add option to generate deactivated users via sampledata (#12772)

parent 710c7323
......@@ -17,9 +17,15 @@ import (
"github.com/icrowley/fake"
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"github.com/spf13/cobra"
)
const (
DEACTIVATED_USER = "deactivated"
GUEST_USER = "guest"
)
var SampleDataCmd = &cobra.Command{
Use: "sampledata",
Short: "Generate sample data",
......@@ -32,6 +38,7 @@ func init() {
SampleDataCmd.Flags().Int("channels-per-team", 10, "The number of sample channels per team.")
SampleDataCmd.Flags().IntP("users", "u", 15, "The number of sample users.")
SampleDataCmd.Flags().IntP("guests", "g", 1, "The number of sample guests.")
SampleDataCmd.Flags().Int("deactivated-users", 0, "The number of deactivated users.")
SampleDataCmd.Flags().Int("team-memberships", 2, "The number of sample team memberships per user.")
SampleDataCmd.Flags().Int("channel-memberships", 5, "The number of sample channel memberships per user in a team.")
SampleDataCmd.Flags().Int("posts-per-channel", 100, "The number of sample post per channel.")
......@@ -165,6 +172,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error {
if err != nil || users < 0 {
return errors.New("Invalid users parameter")
}
deactivatedUsers, err := command.Flags().GetInt("deactivated-users")
if err != nil || deactivatedUsers < 0 {
return errors.New("Invalid deactivated-users parameter")
}
guests, err := command.Flags().GetInt("guests")
if err != nil || guests < 0 {
return errors.New("Invalid guests parameter")
......@@ -283,12 +294,17 @@ func sampleDataCmdF(command *cobra.Command, args []string) error {
allUsers := []string{}
for i := 0; i < users; i++ {
userLine := createUser(i, teamMemberships, channelMemberships, teamsAndChannels, profileImages, false)
userLine := createUser(i, teamMemberships, channelMemberships, teamsAndChannels, profileImages, "")
encoder.Encode(userLine)
allUsers = append(allUsers, *userLine.User.Username)
}
for i := 0; i < guests; i++ {
userLine := createUser(i, teamMemberships, channelMemberships, teamsAndChannels, profileImages, true)
userLine := createUser(i, teamMemberships, channelMemberships, teamsAndChannels, profileImages, GUEST_USER)
encoder.Encode(userLine)
allUsers = append(allUsers, *userLine.User.Username)
}
for i := 0; i < deactivatedUsers; i++ {
userLine := createUser(i, teamMemberships, channelMemberships, teamsAndChannels, profileImages, DEACTIVATED_USER)
encoder.Encode(userLine)
allUsers = append(allUsers, *userLine.User.Username)
}
......@@ -355,36 +371,44 @@ func sampleDataCmdF(command *cobra.Command, args []string) error {
return nil
}
func createUser(idx int, teamMemberships int, channelMemberships int, teamsAndChannels map[string][]string, profileImages []string, guest bool) app.LineImportData {
password := fmt.Sprintf("SampleUs@r-%d", idx)
email := fmt.Sprintf("user-%d@sample.mattermost.com", idx)
if guest {
password = fmt.Sprintf("SampleGu@st-%d", idx)
email = fmt.Sprintf("guest-%d@sample.mattermost.com", idx)
}
func createUser(idx int, teamMemberships int, channelMemberships int, teamsAndChannels map[string][]string, profileImages []string, userType string) app.LineImportData {
firstName := fake.FirstName()
lastName := fake.LastName()
position := fake.JobTitle()
username := fmt.Sprintf("%s.%s", strings.ToLower(firstName), strings.ToLower(lastName))
if guest {
roles := "system_user"
var password string
var email string
switch userType {
case GUEST_USER:
password = fmt.Sprintf("SampleGu@st-%d", idx)
email = fmt.Sprintf("guest-%d@sample.mattermost.com", idx)
roles = "system_guest"
if idx == 0 {
username = "guest"
password = "SampleGu@st1"
email = "guest@sample.mattermost.com"
}
} else if idx == 0 {
username = "sysadmin"
password = "Sys@dmin-sample1"
email = "sysadmin@sample.mattermost.com"
} else if idx == 1 {
username = "user-1"
}
case DEACTIVATED_USER:
password = fmt.Sprintf("SampleDe@ctivated-%d", idx)
email = fmt.Sprintf("deactivated-%d@sample.mattermost.com", idx)
default:
password = fmt.Sprintf("SampleUs@r-%d", idx)
email = fmt.Sprintf("user-%d@sample.mattermost.com", idx)
if idx == 0 {
username = "sysadmin"
password = "Sys@dmin-sample1"
email = "sysadmin@sample.mattermost.com"
} else if idx == 1 {
username = "user-1"
}
position := fake.JobTitle()
roles := "system_user"
if guest {
roles = "system_guest"
} else if idx%5 == 0 {
roles = "system_admin system_user"
if idx%5 == 0 {
roles = "system_admin system_user"
}
}
// The 75% of the users have custom profile image
......@@ -450,10 +474,15 @@ func createUser(idx int, teamMemberships int, channelMemberships int, teamsAndCh
team := possibleTeams[position]
possibleTeams = append(possibleTeams[:position], possibleTeams[position+1:]...)
if teamChannels, err := teamsAndChannels[team]; err {
teams = append(teams, createTeamMembership(channelMemberships, teamChannels, &team, guest))
teams = append(teams, createTeamMembership(channelMemberships, teamChannels, &team, userType == GUEST_USER))
}
}
var deleteAt int64
if userType == DEACTIVATED_USER {
deleteAt = model.GetMillis()
}
user := app.UserImportData{
ProfileImage: profileImage,
Username: &username,
......@@ -470,6 +499,7 @@ func createUser(idx int, teamMemberships int, channelMemberships int, teamsAndCh
MessageDisplay: &messageDisplay,
ChannelDisplayMode: &channelDisplayMode,
TutorialStep: &tutorialStep,
DeleteAt: &deleteAt,
}
return app.LineImportData{
Type: "user",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment