This is for use with the dl-license-keys plugin found here:
I recommend using a hidden setting that indicates whether or not the plugin is licensed. Then you can implement a daily check that revalidates the key to ensure it remains enabled. You can always disable it in the admin console on your site.
So if you include the setting for the license key itself, you’ll end up with something along these lines:
my-plugin/config/settings.yml
plugins:
my_plugin_enabled:
default: true
client: true
my_plugin_license_key:
default: ""
client: false
my_plugin_licensed:
default: false
client: true
hidden: true
my-plugin/config/locales/client.en.yml
site_settings:
my_plugin_enabled: "Enable my awesome plugin!"
my_plugin_license_key: 'Enter your license key to unlock premium features. Get a license key at <a href="https://example.com/premium">example.com/premium</a>.'
I’ve found it easiest to segment my plugin specific code within a ruby engine. Within that engine you’ll need to handle two situations:
- Validate the license key when it’s entered.
- Daily check that the license key remains valid.
The thing to notice here is the line defining the validate_url
. It includes the site where the plugin is installed as well as the product id created on the site. You can get this id number here:
Once you have that number, add it to the validate_url
as the id
parameter in the following:
my-plugin/plugin.rb
# name: my-plugin
# about: Adds awesome functionality.
# version: 0.1
# author: Joe Buhlig joebuhlig.com
# url: https://www.github.com/joebuhlig/my-plugin
enabled_site_setting :my_plugin_enabled
load File.expand_path('../lib/my_plugin/engine.rb', __FILE__)
my-plugin/lib/my_plugin/engine.rb
module MyPlugin
class Engine < ::Rails::Engine
isolate_namespace MyPlugin
config.after_initialize do
Discourse::Application.routes.append do
mount ::MyPlugin::Engine, at: "/my-plugin"
end
module ::Jobs
class MyPluginConfirmValidKey < Jobs::Scheduled
every 1.days
def execute(args)
validate_url = "https://example.com/licenses/validate?id=XXXXX&key=" + SiteSetting.my_plugin_license_key
request = Net::HTTP.get(URI.parse(validate_url))
result = JSON.parse(request)
if result["enabled"]
SiteSetting.my_plugin_licensed = true
else
SiteSetting.my_plugin_licensed = false
end
end
end
end
end
end
end
require 'open-uri'
require 'net/http'
DiscourseEvent.on(:site_setting_saved) do |site_setting|
if site_setting.name.to_s == "my_plugin_license_key" && site_setting.value_changed?
if site_setting.value.empty?
SiteSetting.my_plugin_licensed = false
else
validate_url = "https://example.com/licenses/validate?id=XXXXX&key=" + site_setting.value
request = Net::HTTP.get(URI.parse(validate_url))
result = JSON.parse(request)
if result["errors"]
raise Discourse::InvalidParameters.new(
'Sorry. That key is invalid.'
)
end
if result["enabled"]
SiteSetting.my_plugin_licensed = true
else
SiteSetting.my_plugin_licensed = false
end
end
end
end
You now have a site setting that can be used to constrain certain aspects of your plugin. With that in mind, you’ll want an easy way to use this constraint on both the Ruby and Ember side of things.
my-plugin/lib/my_plugin/my_plugin_licensed_constraint.rb
class MyPluginLicensedConstraint
def matches?(request)
SiteSetting.my_plugin_licensed
end
end
my-plugin/assets/javascripts/discourse/lib/licensed-constraint.js.es6
export function licensed() {
return Discourse.SiteSettings.my_plugin_licensed;
}
Now anytime you want to use these, you can import them into the file and use them within a conditional:
my-plugin/assets/javascripts/discourse/controllers/my-plugin-page-show.js.es6
import { licensed } from 'discourse/plugins/my-plugin/discourse/lib/licensed-constraint';
export default Ember.Controller.extend({
licensed: licensed()
})
my-plugin/assets/javascripts/discourse/templates/my-plugin-page.hbs
{{#if licensed }}
<div>licensed content</div>
{{else}}
<div>NOT licensed content</div>
{{/if}}