Skip to content

Commit

Permalink
UsersExportImport PA 01.02.03
Browse files Browse the repository at this point in the history
- New feature Auto Update Existing users when UserName already exists
  • Loading branch information
fordnn committed Jul 30, 2020
1 parent 9d39def commit d7370ff
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 21 deletions.
9 changes: 4 additions & 5 deletions App_LocalResources/MainControl.ascx.resx
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@
<data name="ImportRoles3.Text" xml:space="preserve">
<value>Import roles by RoleName from the "Roles" column (autocreate Role if it does not exist)</value>
</data>
<<<<<<< HEAD
</root>
=======
</root>
>>>>>>> 7db8571baf53c8d726ec06806d36014c84b8f302
<data name="UpdateExistingUser.Text" xml:space="preserve">
<value>Update existing user when UserName exists in DB</value>
</data>
</root>
64 changes: 54 additions & 10 deletions Components/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Services.Exceptions;

namespace forDNN.Modules.UsersExportImport
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public static string ToCSV(DataTable dt, bool includeHeaderAsFirstRow, string se
csvRows.AppendLine(sb.ToString());
}

foreach(DataRow dr in dt.Rows)
foreach (DataRow dr in dt.Rows)
{
sb = new StringBuilder();
for (int index = 0; index < ColumnsCount; index++)
Expand Down Expand Up @@ -178,7 +179,10 @@ private static DataTable CSV2Table(byte[] lstBytes)
{
dr[k] = lstValues[k];
}
catch { }
catch (Exception Exc)
{
Exceptions.LogException(Exc);
}
}
dt.Rows.Add(dr);
}
Expand Down Expand Up @@ -253,6 +257,8 @@ public static string DoImport(PortalSettings objPortalSettings, string LocalReso
return Localization.GetString("ImportFileMissed", LocalResourceFile);
}

bool UpdateExistingUser = Convert.ToBoolean(objContext.Request.Form["cbUpdateExistingUser"]);

HttpPostedFile objFile = objContext.Request.Files[0];

System.IO.FileInfo objFileInfo = new System.IO.FileInfo(objFile.FileName);
Expand Down Expand Up @@ -297,6 +303,20 @@ public static string DoImport(PortalSettings objPortalSettings, string LocalReso
try
{
DotNetNuke.Entities.Users.UserInfo objUser = new DotNetNuke.Entities.Users.UserInfo();
//use email as username, when username is not provided
objUser.Username = string.Format("{0}", GetDataRowValue(dt, dr, "Username", objUser.Email));

bool UserAlreadyExists = false;
if (UpdateExistingUser)
{
objUser = UserController.GetUserByName(objPortalSettings.PortalId, objUser.Username);
if (objUser != null)
{
UserAlreadyExists = true;
}
}


objUser.Profile.InitialiseProfile(objPortalSettings.PortalId, true);
objUser.Email = string.Format("{0}", dr["Email"]);
objUser.FirstName = string.Format("{0}", dr["FirstName"]);
Expand All @@ -317,9 +337,6 @@ public static string DoImport(PortalSettings objPortalSettings, string LocalReso
continue;
}

//use email as username, when username is not provided
objUser.Username = string.Format("{0}", GetDataRowValue(dt, dr, "Username", objUser.Email));

if (dt.Columns.Contains("Password"))
{
objUser.Membership.Password = string.Format("{0}", dr["Password"]);
Expand All @@ -336,8 +353,16 @@ public static string DoImport(PortalSettings objPortalSettings, string LocalReso
objUser.Membership.PasswordQuestion = objUser.Membership.Password;
objUser.Membership.UpdatePassword = Convert.ToBoolean(objContext.Request.Form["cbForcePasswordChange"]);//update password on next login

DotNetNuke.Security.Membership.UserCreateStatus objCreateStatus =
DotNetNuke.Entities.Users.UserController.CreateUser(ref objUser);
DotNetNuke.Security.Membership.UserCreateStatus objCreateStatus;
if (UserAlreadyExists)
{
objCreateStatus = DotNetNuke.Security.Membership.UserCreateStatus.Success;
}
else
{
objCreateStatus = UserController.CreateUser(ref objUser);
}

if (objCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.Success)
{
if (dt.Columns.IndexOf("UserID") != -1)
Expand Down Expand Up @@ -433,6 +458,7 @@ public static string DoImport(PortalSettings objPortalSettings, string LocalReso
FailedUsers.AppendFormat(Localization.GetString("RowException", LocalResourceFile),
UsersCount,
Exc.Message);
Exceptions.LogException(Exc);
}
}
return string.Format(Localization.GetString("Result", LocalResourceFile),
Expand Down Expand Up @@ -471,6 +497,22 @@ string LocalResourceFile
break;
}

string AdministratorRoleName = "";
try
{
AdministratorRoleName =
(objPortalSettings.AdministratorRoleName == DotNetNuke.Common.Utilities.Null.NullString) ? "" : objPortalSettings.AdministratorRoleName;
}
catch(Exception Exc)
{
Exceptions.LogException(Exc);
}
if (AdministratorRoleName == "")
{
//for child portals its empty and we need RoleName only to check security, so can use any uniq string - always will be false
AdministratorRoleName = System.Guid.NewGuid().ToString();
}

StringBuilder sb = new StringBuilder();
foreach (string Role in Roles.Split(new char[] { ',' }))
{
Expand Down Expand Up @@ -509,7 +551,7 @@ string LocalResourceFile
//check current user has permissions to import user with specific role
if (!
(objCurrentUser.IsInRole(objRole.RoleName) ||
objCurrentUser.IsInRole(objPortalSettings.AdministratorRoleName) ||
objCurrentUser.IsInRole(AdministratorRoleName) ||
objCurrentUser.IsSuperUser
))
{
Expand Down Expand Up @@ -573,6 +615,7 @@ public static string SendEmail(UserInfo objUser, string SubjectTemplate, string
}
catch (Exception Exc)
{
Exceptions.LogException(Exc);
return Exc.Message;
}
return "";
Expand Down Expand Up @@ -643,8 +686,9 @@ public static string GetMaxAllowedFileSize()
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
return string.Format("{0} kB", section.MaxRequestLength);
}
catch
catch (Exception Exc)
{
Exceptions.LogException(Exc);
return "undefined";
}
}
Expand Down Expand Up @@ -682,4 +726,4 @@ public static string ResolveUrl(string FileName, bool VirtualPathOnly)
}

}
}
}
8 changes: 7 additions & 1 deletion Install/_UsersExportImport.dnn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dotnetnuke type="Package" version="5.0">
<packages>
<package name="forDNN.UsersExportImport" type="Module" version="1.2.1">
<package name="forDNN.UsersExportImport" type="Module" version="1.2.3">
<friendlyName>forDNN.UsersExportImport</friendlyName>
<description>This module allows to export/import users, user profiles properties and roles to DNN (former DotNetNuke) from CSV/XML files. This is simple and fast way to create your users accounts in one click.</description>
<owner>
Expand All @@ -25,6 +25,12 @@
</license>
<releaseNotes>
<![CDATA[
<b>Version 1.2.3:</b><br />
New feature Auto Update Existing users when UserName already exists<br />

<b>Version 1.2.2:</b><br />
Fixed an issue in import process for child portals, when no default roles in system (no Admins role)<br />

<b>Version 1.2.1:</b><br />
Added auto create role on roles import.
Tested with recent version of DNN 9.6.2<br />
Expand Down
Binary file added Install/bin/forDNN.Modules.UsersExportImport.dll
Binary file not shown.
Binary file added Install/forDNN.UsersExportImport_v.01.02.02.zip
Binary file not shown.
Binary file not shown.
8 changes: 6 additions & 2 deletions MainControl.ascx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
<asp:ListItem Value="2" resourcekey="ImportRoles2"></asp:ListItem>
</asp:RadioButtonList>
</div>
<div class="dnnFormItem">
<dnn:Label ID="lblUpdateExistingUser" runat="server" resourcekey="UpdateExistingUser"></dnn:Label>
<asp:CheckBox runat="server" ID="cbUpdateExistingUser" CssClass="normalCheckBox" />
</div>
<div class="dnnFormItem">
<dnn:Label ID="lblImportProfileProperties" runat="server" resourcekey="ImportProfileProperties"></dnn:Label>
<asp:CheckBox runat="server" ID="cbImportProfileProperties" CssClass="normalCheckBox" Checked="true" />
Expand Down Expand Up @@ -194,7 +198,8 @@
formData.append('cbCreateMissedProfileProperties', $("#<%=cbCreateMissedProfileProperties.ClientID%>").is(":checked"));
formData.append('cbRandomPassword', $("#<%=cbRandomPassword.ClientID%>").is(":checked"));
formData.append('cbForcePasswordChange', $("#<%=cbForcePasswordChange.ClientID%>").is(":checked"));
formData.append('cbEmailUser', $("#<%=cbEmailUser.ClientID%>").is(":checked"));
formData.append('cbEmailUser', $("#<%=cbEmailUser.ClientID%>").is(":checked"));
formData.append('cbUpdateExistingUser', $("#<%=cbUpdateExistingUser.ClientID%>").is(":checked"));
var sf = $.ServicesFramework(moduleId);
var serviceUrl = sf.getServiceRoot('forDNN.UsersExportImport') + "ExportImport/DoImport";
Expand Down Expand Up @@ -237,4 +242,3 @@
});
}
</script>

2 changes: 1 addition & 1 deletion MainControl.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using DotNetNuke.UI.UserControls;

using DotNetNuke.Security.Roles;


namespace forDNN.Modules.UsersExportImport
{
Expand Down
18 changes: 18 additions & 0 deletions MainControl.ascx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Models/ExportInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Portals;

namespace forDNN.Modules.UsersExportImport.Models
{
public class ExportInfo
{
public int ExportFileType { get; set; }
public bool IncludeSuperUsers { get; set; }
public bool IncludeDeleted { get; set; }
public bool IncludeNonAuthorised { get; set; }
public bool ExportRoles { get; set; }
public bool ExportPasswords { get; set; }
public string PropertiesToExport { get; set; }
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("forDNN")]
[assembly: AssemblyProduct("Users Export/Import for DNN (former DotNetNuke)")]
[assembly: AssemblyCopyright("(c) forDNN 2003-2015")]
[assembly: AssemblyCopyright("(c) forDNN 2003-2020")]
[assembly: AssemblyTrademark("")]
[assembly: GuidAttribute("a9a2f906-2bbe-44fa-9c6d-4c3137d872a8")]

Expand All @@ -29,4 +29,4 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.2.1.*")]
[assembly: AssemblyVersion("1.2.3.*")]
49 changes: 49 additions & 0 deletions WebApi/ExportImportApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using DotNetNuke.Web.Api;
using DotNetNuke.Security;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace forDNN.Modules.UsersExportImport.WebApi
{
[SupportedModules("forDNN.UsersExportImport")]
public class ExportImportController : DnnApiController
{
[HttpPost]
[ActionName("DoExport")]
[ValidateAntiForgeryToken]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
public HttpResponseMessage DoExport([FromBody]Models.ExportInfo objValues)
{
try
{
return Request.CreateResponse(HttpStatusCode.OK, Controller.ExportController.DoExport(this.PortalSettings.PortalId, objValues));
}
catch (System.Exception ex)
{
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}

[HttpPost]
[ActionName("DoImport")]
[ValidateAntiForgeryToken]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
public HttpResponseMessage DoImport()
{
try
{
string LocalResourceFile = CommonController.ResolveUrl("App_LocalResources/MainControl", true);
string Result = CommonController.DoImport(this.PortalSettings, LocalResourceFile, this.UserInfo);
return Request.CreateResponse(HttpStatusCode.OK, Result);
}
catch (System.Exception ex)
{
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}

}
}
22 changes: 22 additions & 0 deletions WebApi/RouteMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using DotNetNuke.Web.Api;

namespace forDNN.UsersExportImport.WebApi
{
/// <summary>
/// Class Routemapper.
/// </summary>
public class Routemapper : IServiceRouteMapper
{
/// <summary>
/// Registers the routes.
/// </summary>
/// <param name="routeManager">The route manager.</param>
public void RegisterRoutes(IMapRoute routeManager)
{
routeManager.MapHttpRoute("forDNN.UsersExportImport", "default", "{controller}/{action}",
new[] { "forDNN.Modules.UsersExportImport.WebApi" });
}
}
}


0 comments on commit d7370ff

Please sign in to comment.