Skip to content

Commit

Permalink
Merge pull request #46 from UpendoVentures/feature-set-demousers
Browse files Browse the repository at this point in the history
Feature set demousers
  • Loading branch information
WillStrohl authored Aug 15, 2023
2 parents 49b40b3 + 112b5a1 commit 7672a60
Show file tree
Hide file tree
Showing 11 changed files with 548 additions and 11 deletions.
60 changes: 60 additions & 0 deletions Modules/UpendoPrompt/App_LocalResources/Global.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@
<data name="CaptchaModeToggled.Text" xml:space="preserve">
<value>CAPTCHA has been toggled {0} on this website. </value>
</data>
<data name="CreateUserExists.Text" xml:space="preserve">
<value>User already exists, not created: {0} ({1}).</value>
</data>
<data name="CreateUserFailure.Text" xml:space="preserve">
<value>Error creating user: {0} ({1}).</value>
</data>
<data name="CreateUserSuccess.Text" xml:space="preserve">
<value>Successfully created user: {0} ({1}).</value>
</data>
<data name="DebugDisabled.Text" xml:space="preserve">
<value>Debugging is disabled throughout the site. </value>
</data>
Expand All @@ -141,6 +150,24 @@
<data name="DebugStatusWebConfig.Text" xml:space="preserve">
<value>Debugging in the web.config is {0}.</value>
</data>
<data name="DeleteDemoUsers.Text" xml:space="preserve">
<value>Allows you to delete the user accounts created using the `set-demousers` command, in bulk.</value>
</data>
<data name="DeleteUserNotNeeded.Text" xml:space="preserve">
<value>User account does not exist, not deleted: {0} {1} ({2}).</value>
</data>
<data name="DeleteUserSuccess.Text" xml:space="preserve">
<value>Successfully deleted user account: {0} {1} ({2}).</value>
</data>
<data name="DemoUserAccountsAdded.Text" xml:space="preserve">
<value>Successfully added demo user accounts... We saved some time together. Yay! :)</value>
</data>
<data name="DemoUsers.Text" xml:space="preserve">
<value>Helps when developing or building out demos, by automatically adding a bunch of fun user accounts to play with.</value>
</data>
<data name="DemoUsersDeleted.Text" xml:space="preserve">
<value>Successfully deleted demo user accounts... We saved some time together. Yay! :)</value>
</data>
<data name="Deprecated.Text" xml:space="preserve">
<value>This command has been deprecated. Please use '{0}' instead. This command will be removed in a future release.</value>
</data>
Expand Down Expand Up @@ -265,6 +292,21 @@
&lt;code class="block"&gt;list-debug&lt;/code&gt;
&lt;/div&gt;</value>
</data>
<data name="Prompt_DeleteDemoUsers_ResultHtml.Text" xml:space="preserve">
<value>&lt;div&gt;
&lt;p&gt;This is a companion command for the `set-demousers` command. This version of the command will permanently delete the users accounts created by that command.&lt;/p&gt;
&lt;h4&gt;Using delete-demousers&lt;/h4&gt;
&lt;code class="block"&gt;delete-demousers&lt;/code&gt;
&lt;p&gt;This command does not have any flags&lt;/p&gt;
&lt;code class="block"&gt;delete-demousers&lt;/code&gt;
&lt;h4&gt;Output&lt;/h4&gt;
&lt;p&gt;If successful, you should see a message for each demo user deleted, stating the result of the deletion of the account.&lt;/p&gt;
&lt;p&gt;User Deleted:&lt;/p&gt;
&lt;code class="block"&gt;Successfully deleted user account: Jane Doe ({jdoe@example.com}).&lt;/code&gt;
&lt;p&gt;User Doesn't Exist:&lt;/p&gt;
&lt;code class="block"&gt;User account does not exist, not deleted: Jane Doe ({jdoe@example.com}).&lt;/code&gt;
&lt;/div&gt;</value>
</data>
<data name="Prompt_DeletePackages_ResultHtml.Text" xml:space="preserve">
<value>&lt;div&gt;
&lt;p&gt;Newer versions of DNN saves any installed extension into the `/App_Data/ExtensionPackages/` folder. This
Expand Down Expand Up @@ -301,6 +343,24 @@
&lt;/p&gt;
&lt;/div&gt;</value>
</data>
<data name="Prompt_DemoUsers_ResultHtml.Text" xml:space="preserve">
<value>&lt;div&gt;
&lt;p&gt;When working on building a website or extension, you often need some demo users to use for your work. This command adds a handful of test user accounts for you.&lt;/p&gt;
&lt;h4&gt;Using set-demousers&lt;/h4&gt;
&lt;code class="block"&gt;set-demousers&lt;/code&gt;
&lt;p&gt;This command does not have any flags&lt;/p&gt;
&lt;code class="block"&gt;set-demousers&lt;/code&gt;
&lt;h4&gt;Output&lt;/h4&gt;
&lt;p&gt;If successful, you should see a message for each demo user, stating the result of the creation of the account.&lt;/p&gt;
&lt;p&gt;User Created:&lt;/p&gt;
&lt;code class="block"&gt;Successfully created user: Jane Doe ({jdoe@example.com}).&lt;/code&gt;
&lt;p&gt;User Exists:&lt;/p&gt;
&lt;code class="block"&gt;User already exists, not created: Jane Doe ({jdoe@example.com}).&lt;/code&gt;
&lt;p&gt;Error Creating User:&lt;/p&gt;
&lt;code class="block"&gt;Error creating user: Jane Doe ({jdoe@example.com}).&lt;/code&gt;
&lt;p&gt;(&lt;em&gt;if this happens, check the Admin Log to see why, or run `set-debug` and then check the Log4NET log file for more details.&lt;/em&gt;)&lt;/p&gt;
&lt;/div&gt;</value>
</data>
<data name="Prompt_ImpersonateFlagsInvalid.Text" xml:space="preserve">
<value>You must either specify a valid UserID -OR- Username value to impersonate another user account. Both were empty. Please only specify one.</value>
</data>
Expand Down
192 changes: 192 additions & 0 deletions Modules/UpendoPrompt/Commands/DeleteDemoUsers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#region License

// Distributed under the MIT License
// ============================================================
// Copyright (c) Upendo Ventures, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#endregion

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.UI;
using DotNetNuke.Instrumentation;
using Upendo.Modules.UpendoPrompt.Components;
using Constants = Upendo.Modules.UpendoPrompt.Components.Constants;

// required for DNN Prompt
//using DotNetNuke.Prompt; // deprecated Prompt is replaced with this
using Dnn.PersonaBar.Library.Helper;
using Dnn.PersonaBar.Library.Prompt;
using Dnn.PersonaBar.Library.Prompt.Attributes;
using Dnn.PersonaBar.Library.Prompt.Models;
using DotNetNuke.Common.Utilities; // added for the commented-out clearing of cache below
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using Upendo.Modules.UpendoPrompt.Entities;

namespace Upendo.Modules.UpendoPrompt.Commands
{
[ConsoleCommand("delete-demousers", Constants.PromptCategory, "DeleteDemoUsers")]
public class DeleteDemoUsers : PromptBase, IConsoleCommand
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(DeleteDemoUsers));

#region Implementation

public override void Init(string[] args, PortalSettings portalSettings, UserInfo userInfo, int activeTabId)
{
// no paramters
}

public override ConsoleResultModel Run()
{
try
{
var messages = new List<PromptMessage>();

messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Tony",
LastName = "Stark",
Email = "edith@example.com",
Username = "tstark"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Diana",
LastName = "Prince",
Email = "dembootstho@example.com",
Username = "dprince"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Peter",
LastName = "Quill",
Email = "gamoralover@example.com",
Username = "pquill"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Rey",
LastName = "Skywalker",
Email = "sithlight@example.com",
Username = "rskywalker"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Ethan",
LastName = "Hunt",
Email = "possible@example.com",
Username = "ehunt"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Wade",
LastName = "Wilson",
Email = "newguy@example.com",
Username = "wwilson"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Natasha",
LastName = "Romanov",
Email = "boss@example.com",
Username = "nromanov"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Hermione",
LastName = "Granger",
Email = "wanded@example.com",
Username = "hgranger"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "John",
LastName = "Wick",
Email = "ripmrwick@example.com",
Username = "jwick"
}));
messages.Add(DeleteUserAccount(new DemoUser
{
FirstName = "Eggsy",
LastName = "Unwin",
Email = "gentleman@example.com",
Username = "eunwin"
}));

// emp

var output = this.LocalizeString(Constants.LocalizationKeys.DemoUsersDeleted);

// clear DNN cache
//DataCache.ClearCache();

return new ConsoleResultModel
{
Output = output,
Data = messages,
IsError = false
};
}
catch (Exception e)
{
LogError(e);
return new ConsoleErrorResultModel(string.Concat(Constants.OutputPrefix, this.LocalizeString(Constants.LocalizationKeys.ErrorOccurred)));
}
}

#endregion

#region Helpers

private PromptMessage DeleteUserAccount(DemoUser newUser)
{
var OUser = DotNetNuke.Entities.Users.UserController.GetUserByName(this.PortalId, newUser.Username);
var blnExists = (OUser != null);

if (blnExists)
{
// delete the user account
DotNetNuke.Entities.Users.UserController.DeleteUser(ref OUser, false, false);
DotNetNuke.Entities.Users.UserController.RemoveUser(OUser);
return new PromptMessage(string.Format(this.LocalizeString(Constants.LocalizationKeys.DeleteUserSuccess), newUser.FirstName, newUser.LastName, newUser.Email));
}

// message about the user not needing to be deleted
return new PromptMessage(string.Format(this.LocalizeString(Constants.LocalizationKeys.DeleteUserNotNeeded), newUser.FirstName, newUser.LastName, newUser.Email));
}

protected override void LogError(Exception ex)
{
if (ex != null)
{
Logger.Error(ex.Message, ex);
if (ex.InnerException != null)
{
Logger.Error(ex.InnerException.Message, ex.InnerException);
}
}
}
#endregion
}
}
Loading

0 comments on commit 7672a60

Please sign in to comment.