Code redemption aka user self-help role adding
Thought I shared this before, but I couldn't find an older post, so here goes.
Got tired of staff emailing, asking to be given access, forgetting or giving the wrong username.
So I put this together.
The user gets a code from us, visits this custom community page, enters their code and is given a specific role. The code they used is then rendered inactive.
I generated two sets of random codes and stuck them in two separate forum posts in a hidden area.
(You could also ask Lithium to create a custom database field and read that.)
One forum post per type of code you want to check.
With this script we can give a user one of two kinds of codes
- give moderator and staff access
- give just staff access
And let me tell you, this has saved us soooo much time and frustration.
Just stick this on a custom page and you're basically good to go.
<#assign staffPostId = 12345/> <#-- Unique ID of the post that contains all the generated codes for Staff access--> <#assign ModPostId = 67891/> <#-- Unique ID of the post that contains all the generated codes for Moderator access--> <#assign codeFoundStaff = false/> <#assign codeFoundMod = false/> <#if !user.anonymous> <#assign code = http.request.parameters.name.get("code", "")?string /> <#if code == ""> <div class="lia-inline-ajax-feedback"> <div class="AjaxFeedback" id="feedback"></div> </div> <div class="lia-form-fieldset-wrapper" id="fieldset"> <fieldset class=""> <legend>Community Code Redemption</legend> <div class="lia-quilt-row lia-quilt-row-standard lia-input-edit-form-row"> <div class="lia-quilt-column lia-quilt-column-24 lia-quilt-column-single lia-input-edit-form-column"> <div class="lia-quilt-column-alley lia-quilt-column-alley-single"> <div class="lia-form-row lia-form-in-code-entry"> <div class="lia-quilt-row lia-quilt-row-standard"> <div class="lia-quilt-column lia-quilt-column-06 lia-quilt-column-left"> <div class="lia-quilt-column-alley lia-quilt-column-alley-left"> <div class="lia-form-label-wrapper"><label for="lia-inCode" class="lia-form-label">Code</label></div> </div> </div> <div class="lia-quilt-column lia-quilt-column-18 lia-quilt-column-right"> <div class="lia-quilt-column-alley lia-quilt-column-alley-right"> <div class="lia-quilt-row lia-quilt-row-standard"> <div class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-single"> <div class="lia-quilt-column-alley lia-quilt-column-alley-single"> <div class="lia-form-input-wrapper"> <input class="lia-form-type-text" id="lia-inCode" name="inCode" placeholder="Enter your secret code and await redemption"> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> <div class="lia-quilt-row lia-quilt-row-standard lia-form-buttons-empty-left-column"> <div class="lia-quilt-column lia-quilt-column-06 lia-quilt-column-left"> <div class="lia-quilt-column-alley lia-quilt-column-alley-left"> </div> </div> <div class="lia-quilt-column lia-quilt-column-18 lia-quilt-column-right"> <div class="lia-quilt-column-alley lia-quilt-column-alley-right"> <div class="lia-form-submit lia-button-group active"> <span class="lia-button-wrapper lia-button-wrapper-primary lia-button-wrapper-Submit-action"><input title="" class="lia-button lia-button-primary lia-button-Submit-action" value="Submit" id="submitCode" name="submitContext" type="submit"></span> </div> </div> </div> </div> <div class="lia-quilt-row lia-quilt-row-standard lia-input-edit-form-row lia-quilt-row-embed hidden"> <div class="lia-quilt-column lia-quilt-column-24 lia-quilt-column-single lia-input-edit-form-column"> <div class="lia-quilt-column-alley lia-quilt-column-alley-single"> <div class="lia-form-row lia-form-embed"> <div class="lia-quilt-row lia-quilt-row-standard"> <div class="lia-quilt-column lia-quilt-column-06 lia-quilt-column-left"> <div class="lia-quilt-column-alley lia-quilt-column-alley-left"> <div class="lia-form-label-wrapper"><label for="lia-embed" class="lia-form-label" id="lbl-entityTitle"></label></div> </div> </div> <div class="lia-quilt-column lia-quilt-column-18 lia-quilt-column-right"> <div class="lia-quilt-column-alley lia-quilt-column-alley-right"> <div class="lia-quilt-row lia-quilt-row-standard"> <div class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-single"> <div class="lia-quilt-column-alley lia-quilt-column-alley-single"> <div class="lia-form-input-wrapper" id="embed"> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </fieldset> </div> <style type="text/css"> .lia-quilt-row-available { margin-top:20px; padding-top:20px; border-top:2px solid #efefef; } textarea#lia-missingCountries, textarea#lia-availableCountries{ resize:none; height:100px; } </style> <@liaAddScript> $("#submitCode").on("click", function() { var userCode = $("#lia-inCode").val(); var userCode = userCode.replace(" ",""); if( userCode != "") { window.open('//' + location.host + location.pathname + '?code=' + userCode,'_self'); } }); </@liaAddScript> <#else> <#assign codesStaff = restadmin("/messages/id/${staffPostId}/body").value?replace("<p>","","i")?replace("</p>","","i")/> <#list codesStaff?split(",") as c> <#if c?string == code?string> <#assign codeFoundStaff = true/> <#assign roles = restadmin("/roles").roles.role /> <#list roles as role> <#if role.name == "Staff"> <#assign hrefRoleStaff = role.@href /> </#if> </#list> <#assign result = restadmin("${hrefRoleStaff}/users/add?role.user=id/${user.id}") /> <#if result.error?has_content> 'Twas not meant to be. Please contact <a href="mailto:community@abccompany.com">community@abccompany.com</a>. <#-- Put any message you want the user to see if their code fails --> <#else> <#assign codesStaffNew = codesStaff?replace("${c},","")?replace(",${c}","")?replace("${c}","")/> <#assign result = restadmin("/messages/id/${staffPostId}/edit?message.body=${codesStaffNew}")/> You were deemed worthy and shall forever be identified as Staff! </#if> <#break> </#if> </#list> <#if codeFoundStaff == false> <#assign codesMod = restadmin("/messages/id/${ModPostId}/body").value?replace("<p>","","i")?replace("</p>","","i")/> <#list codesMod?split(",") as c> <#if c?string == code?string> <#assign codeFoundMod = true/> <#assign roles = restadmin("/roles").roles.role /> <#list roles as role> <#if role.name == "Moderator"> <#assign hrefRoleMod = role.@href /> </#if> <#if role.name == "Staff"> <#assign hrefRoleStaff = role.@href /> </#if> </#list> <#assign result = restadmin("${hrefRoleMod}/users/add?role.user=id/${user.id}") /> <#assign result2 = restadmin("${hrefRoleStaff}/users/add?role.user=id/${user.id}") /> <#if result.error?has_content || result2.error?has_content> Alas, only the penitent will pass; we could not oblige your request. Please contact <a href="mailto:community@abccompany.com">community@abccompany.com</a>. <#-- Put any message you want the user to see if their code fails --> <#else> <#assign codesModNew = codesMod?replace("${c},","")?replace(",${c}","")?replace("${c}","")/> <#assign result = restadmin("/messages/id/${ModPostId}/edit?message.body=${codesModNew}")/> You were deemed worthy and shall forever be identified as Staff and Master of Moderation. </#if> <#break> </#if> </#list> </#if> <#if codeFoundStaff == false && codeFoundMod == false> Unfortunately that code didn't work. Please <span class="back">retrace your steps</span> and try again, or contact <a href="mailto:community@abccompany.com">community@abccompany.com</a>. <#-- Put any message you want the user to see if their code fails --> <@liaAddScript> $(document).ready( function() { $(".back").click( function() { window.open('//' + location.host + location.pathname,'_self'); }); }); </@liaAddScript> </#if> </#if> <#else> We don't know who you are, stranger. Please <a href="${webUi.getUserLoginPageUrl("")}">stay a while</a> and listen, or <a href="${webUi.getUserRegistrationPageUrl("")}">submit your name in the registry</a>. </#if>