Forked from stevewithington/muraImportUsersViaCSV.cfm
Last active
January 16, 2020 19:46
-
-
Save petrichor8/7f0fdd0ce40b6132d866105b7309fc12 to your computer and use it in GitHub Desktop.
Example of how to import Users into Mura CMS via .CSV file. Also see https://gist.github.com/stevewithington/4742829 to import content from an RSS Feed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
param name='form.csvUrl' default='#getPageContext().getRequest().getScheme()#://#cgi.server_name##getDirectoryFromPath(getPageContext().getRequest().getRequestURI())#users.csv'; | |
param name='form.group' default='TempImport'; | |
param name='form.isSubmitted' default='false'; | |
param name='form.isTest' default='true'; | |
param name='form.siteid' default='default'; | |
param name='form.userPass' default=''; | |
$ = application.serviceFactory.getBean('$').init(form.siteid); | |
if ( !$.currentUser().isSuperUser() && !$.currentUser().isInGroup('admin') ) { | |
WriteOutput('<h3>You should not be here.</h3>'); | |
abort; | |
} | |
errors = []; | |
rsSites = $.getBean('settingsManager').getList(); | |
rsGroups = $.getBean('userManager').getUserGroups(form.siteId,1); | |
group = $.getBean('user').loadBy(groupname=form.group); | |
</cfscript> | |
<cfif form.isSubmitted and IsValid('url', form.csvUrl)> | |
<cftry> | |
<cfhttp name="rsUsers" method="get" url="#form.csvUrl#" /> | |
<cfcatch> | |
<cfset ArrayAppend(errors, 'The .CSV file either does not exist at the specified URL, or the file itself is not a valid .CSV file.')> | |
</cfcatch> | |
</cftry> | |
<cfif not ArrayLen(errors)> | |
<cfset arrColumns = ListToArray(rsUsers.columnList)> | |
</cfif> | |
</cfif> | |
<cfoutput> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<cfheader name="expires" value="#dateformat(now(), 'ddd, dd mmm yyyy')# #timeformat(now(), 'HH:mm:ss tt')#"> | |
<cfheader name="pragma" value="no-cache"> | |
<cfheader name="cache-control" value="no-cache, no-store"> | |
<meta http-equiv="pragma" content="no-cache"> | |
<meta http-equiv="Expires" content="#dateformat(now(), 'ddd, dd mmm yyyy')# #timeformat(now(), 'HH:mm:ss tt')#"> | |
<title>Import Users Into Mura CMS Via CSV</title> | |
<style type="text/css"> | |
.wrap { | |
clear:both; | |
display:block; | |
padding:1em; | |
margin:1em; | |
border:1px dashed grey; | |
font-family:Arial, Helvetica, sans-serif; | |
font-size:0.8em; | |
} | |
.wrap label, .wrap input { | |
clear:both; | |
display:block; | |
} | |
.wrap label { | |
padding:1em 0 0 0; | |
} | |
.error { | |
border: 2px solid red; | |
padding: 1em; | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="wrap"> | |
<h2>Import Users Into Mura CMS Via CSV</h2> | |
<cfif form.isSubmitted and IsDefined('rsUsers')> | |
<cftry> | |
<cfset tickStart = getTickCount()> | |
<cfscript> | |
if ( Len(form.group) && group.getIsNew() ) { | |
WriteOutput('<h3>Group (#HTMLEditFormat(form.group)#) does not exist.</h3>'); | |
} | |
invalidUsers = 0; | |
</cfscript> | |
<cfloop query="#rsUsers#"> | |
<cfset localErrors = [] /> | |
<cfscript> | |
// loading by USERNAME. be sure to edit this to match your field names | |
user = $.getBean('user').loadBy(username = rsUsers['Email'][currentrow], siteId = form.siteId ); | |
if ( len( user.getEmail() ) ) { | |
ArrayAppend(localErrors, 'User already exists [#rsUsers['Email'][currentRow]#], will not be imported.'); | |
} | |
// needed to set up Public user (Site Member) | |
user.setValue('Type', 2); // 1=Group, 2=User | |
user.setValue('isPublic', 1); // 0=Private/System, 1=Public/Member | |
user.setValue('InActive', 0); | |
// Make sure the following fields exist at a minimum: FName, LName, Email | |
if ( !ArrayFindNoCase(arrColumns, 'FName') ) { | |
ArrayAppend(localErrors, 'COLUMN: FName is required and was not found in the CSV file.'); | |
} | |
if ( !ArrayFindNoCase(arrColumns, 'LName') ) { | |
ArrayAppend(localErrors, 'COLUMN: LName is required and was not found in the CSV file.'); | |
} | |
if ( !ArrayFindNoCase(arrColumns, 'Email') ) { | |
ArrayAppend(localErrors, 'COLUMN: Email is required and was not found in the CSV file.'); | |
} else if ( !IsValid('email', rsUsers['Email'][currentRow]) ) { | |
ArrayAppend(localErrors, 'Invalid email address: #rsUsers['Email'][currentRow]#. This user was not saved.'); | |
invalidUsers++; | |
} | |
// PARSE .CSV COLUMNS | |
if ( !ArrayLen(localErrors) ) { | |
// If a Username field doesn't exist, use the Email address | |
if ( !ArrayFindNoCase(arrColumns, 'Username') ) { | |
user.setValue('username', rsUsers['Email'][currentrow]); | |
} | |
// If a columnName exists in Mura, it will be populated (e.g., any extended attributes) | |
for ( columnName in arrColumns ) { | |
if ( columnName != 'Groups' ) { | |
user.setValue(columnName, rsUsers[columnName][currentRow]); | |
} | |
} | |
//set password | |
user.setPassword(form.userPass); | |
} | |
// POPULATE GROUPS | |
if ( !ArrayLen(localErrors) ) { | |
if ( ArrayFindNoCase(arrColumns, 'Groups') ) { | |
arrGroups = ListToArray(rsUsers['Groups'][currentRow]); | |
for ( groupName in arrGroups ) { | |
group = $.getBean('user').loadBy(groupname=groupName); | |
if ( !group.getIsNew() ) { | |
user.setGroupID(groupid=group.getUserID(), append=true); | |
} else { | |
ArrayAppend(localErrors, 'GROUP: #groupName# does exist.'); | |
} | |
} | |
} else if ( !group.getIsNew() ) { | |
// use the Group field form the FORM | |
user.setGroupID(groupid=group.getUserID(), append=true); | |
} | |
} | |
// SAVE THE USER | |
if( !ArrayLen(localErrors) ) { | |
user.validate(); | |
if(user.hasErrors() ){ | |
ArrayAppend(localErrors, user.getErrors()); | |
} else { | |
if ( !form.isTest && !ArrayLen(localErrors) ) { | |
user.save(); | |
if ( user.hasErrors() ) { | |
ArrayAppend(localErrors, user.getErrors()); | |
} | |
//WriteDump(user.getAllValues()); | |
} | |
} | |
} | |
if( arrayLen( localErrors) ){arrayAppend(errors,localErrors);} | |
</cfscript> | |
</cfloop> | |
<cfif form.isTest> | |
<h3>Test Results <a href="#CGI.script_name##CGI.query_string#">Return to form ></a></h3> | |
<cfdump var="#rsUsers#" label="rsUsers"> | |
<cfelse> | |
<h3>Completed! <a href="#CGI.script_name##CGI.query_string#">Return to form ></a></h3> | |
<h4>#rsUsers.recordcount-invalidUsers# Users Imported</h4> | |
</cfif> | |
<cfset tickEnd = getTickCount()> | |
<p><em>Processed in #tickEnd-tickStart# milliseconds</em></p> | |
<cfcatch type="any"> | |
<cfset ArrayAppend(errors, cfcatch ) /> | |
</cfcatch> | |
</cftry> | |
<cfelse> | |
<form name="frmUser" method="post"> | |
<label for="csvurl">CSV URL:</label> | |
<input type="text" name="csvurl" id="csvurl" value="#form.csvurl#" size="80" /> | |
<label for="siteid">Site ID:</label> | |
<select name="siteid"> | |
<cfloop query="rsSites"> | |
<option value="#siteid#"<cfif form.siteid eq siteid> selected="selected"</cfif>>#HTMLEditFormat(site)#</option> | |
</cfloop> | |
</select> | |
<label for="group">Group <small>(do NOT use if you have a 'Groups' field in your .CSV file)</small>:</label> | |
<input type="text" name="group" id="group" value="#form.group#" size="80" /> | |
<cfdump var="#rsGroups#" label="All groups under this siteId [#form.siteId#]" /> | |
<label for="userPass">Default Password:</label> | |
<input type="text" name="userPass" id="userPass" value="#form.userPass#" size="80" /> | |
<label for="istest">Test?</label> | |
<select name="istest"> | |
<option value="true"<cfif form.istest> selected="selected"</cfif>>Yes</option> | |
<option value="false"<cfif !form.istest> selected="selected"</cfif>>No</option> | |
</select> | |
<input type="hidden" name="isSubmitted" value="true" /> | |
<p><input type="submit" value="Submit" /></p> | |
</form> | |
</cfif> | |
<!--- ERROR OUTPUT ---> | |
<cfif ArrayLen(errors)> | |
<div class="alert error"> | |
<h4>Error<cfif ArrayLen(errors) gt 1>s</cfif></h4> | |
<ul> | |
<cfloop array="#errors#" index="error"> | |
<li> | |
<cfif IsSimpleValue(error)> | |
#error# | |
<cfelse> | |
<cfdump var="#error#" /> | |
</cfif> | |
</li> | |
</cfloop> | |
</ul> | |
</div> | |
</cfif> | |
</div> | |
</body> | |
</html> | |
</cfoutput> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Company | JobTitle | FName | LName | Age | Groups | ||
---|---|---|---|---|---|---|---|
PODI | Prez | john.doe@email.com | John | Doe | 42 | Members,Stats Viewer | |
PODI | CFO | jane.doe@email.com | Jane | Doe | 38 | Members | |
steve.smith@email.com | Steve | Smith | 29 | Members,Stats Viewer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't include this file into any content item!
Place the file under a folder (_importUsers) in your Mura root, and csv file alongside it.
For example: http://yourdomain.com/_importUsers/muraImportUsersViaCSV.cfm
Then you should be able to navigate directly to that file.