Redmine plugin to send email on certain status change - redmine

My scenario an issue change status in "closed" and redmine must send an email to an email address
My constraint:
issue assignee could not change
email must be configurable by user administration
only one email for issue
I'm currently using Redmine 2.4.3 final and because I'm not able to find a setting or existing plugin to get this done, I'm writting down a plugin, here my code:
require 'mailer'
module IssueAssignToPlugin
class Hooks < Redmine::Hook::ViewListener
def controller_issues_edit_before_save(context={})
configuration = Setting.plugin_helloworldredmineplugin
if !configuration.nil? && configuration['enabled'] && !configuration['user_id'].blank? && !configuration['status_id'].blank?
issue = context[:issue]
status = IssueStatus.find(configuration['status_id'])
user = User.find(configuration['user_id'])
if !user.nil? && !issue.watched_by?(user)
if issue.status == status
#issue.add_watcher(user)
#journal = issue.journals.last
#journal = Journal.new(:journalized => issue, :user => user, :created_on => Time.now)
#journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'label_relates_to', :value => 2)
#Mailer.deliver_issue_edit(journal)
#mail(to: user.mail, cc: user.mail, bbc: user.mail)
#Mailer.deliver_mail(mail)
#
#Your code goes here!
#
end
end
end
return ''
end
alias_method :controller_issues_bulk_edit_before_save, :controller_issues_edit_before_save
alias_method :controller_issues_new_before_save, :controller_issues_edit_before_save
end
end
As you can see I've tried many ways, but without luck.
Could someone please help me? My problem is "just" send an Email having only user.mail and using Redmine "settings"

Related

Devise lockable and custom strategy

I am developing a custom strategy for LDAP authentication, and I have implemented the valid? and authenticate! methods as per http://kyan.com/blog/2013/10/11/devise-authentication-strategies and this Devise wiki. My custom strategy works by itself.
I also want to use the Lockable module (and other Devise modules), but they seem to work only when Database_Authenticatable is also present. I have been reading the Devise source code, but I do not understand how Lockable is able to find the user (resource) in order to increment the :failed_attempts count. How do I pass the user record or resource to Lockable using my custom strategy?
My custom strategy is below. I am using Devise 3.5 and Rails 4.2
require 'net/ldap'
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class LdapAuthenticatable < Authenticatable
def valid?
params[:user] && login.present? && password.present?
end
def authenticate!
resource = mapping.to.find_for_authentication(email: params[:user][:email])
if params[:user]
begin
ldap = Net::LDAP.new
ldap.auth(my_ldap_service, my_ldap_service_password)
ldap.encryption(:simple_tls)
ldap.base = "base_here"
ldap.host = my_ldap_host
ldap.port = "636"
result_attrs = ["employeenumber", "givenname", "sn", "mail", "department"]
result = ldap.bind_as(base: "base_here", filter: "(mail=#{login})", attributes: result_attrs, password: password, time: 3)
if result
#valid ldap credentials
user = get_user_from_database(result)
if user
success!(user)
else
fail!(:invalid_login)
end
else
fail!(:invalid_login)
end
rescue => e
Rails.logger.error e.message
fail!(:invalid_login)
end
end
end
def login
params[:user][:email]
end
def password
params[:user][:password]
end
end
end
end
Active Directory should take care of a lockable feature. By using LDAP in your custom strategy, you move responsibility for the feature from your application to LDAP/Active Directory. Lockable should prevent a villain from brute-forcing access to your application. If Active Directory had no restrictions on this, he could simply run his attack directly against it and in case of success use the credentials in your application.
You should however enable your application to display error messages returned by the remote server like "Too many retries, try again in 5 minutes".
I ended up using devise_custom_authenticatable in combination with the resources posted on the original question. For lockable to work, database_authenticable must be present. Toni's answer above should work if you have control over LDAP, which I did not when I posed the question.
You are missing to call the validate method which is part of Authenticatable class you inherit from. The validate method calls the valid_for_authentication? method from the lockable module which then takes care of incrementing the attempts and lock the account.
In database_authenticatable this is solved here by using valid_password? which returns a boolean. In your case you would need to add a validate(resource) { false } in the case bind_as doesn't return a result.
The fixed full authenticate! method would look like the following
def authenticate!
resource = mapping.to.find_for_authentication(email: params[:user][:email])
if params[:user]
begin
ldap = Net::LDAP.new
ldap.auth(my_ldap_service, my_ldap_service_password)
ldap.encryption(:simple_tls)
ldap.base = "base_here"
ldap.host = my_ldap_host
ldap.port = "636"
result_attrs = ["employeenumber", "givenname", "sn", "mail", "department"]
result = ldap.bind_as(base: "base_here", filter: "(mail=#{login})", attributes: result_attrs, password: password, time: 3)
if result
#valid ldap credentials
user = get_user_from_database(result)
if user
success!(user)
else
fail!(:invalid_login)
end
else
# no result -> wrong password
validate(resource) { false }
fail!(:invalid_login)
end
rescue => e
Rails.logger.error e.message
fail!(:invalid_login)
end
end
end

Pass two params in a Rails Controller

I'm working on a project that requires a third party (not a registered user of my application) to follow an email link to update a document. I've created a :token column for each entry, and have it successfully appended to the email link using:
edit_form_url(:id => #form.id, :token => #form.token)
My problem is trying to reference the additional param in my controller using this code:
def edit
#form = Form.find(params[:id],[:token])
redirect_to pages_error_path if #form.sign.present?
end
When using the above code, I get a Couldn't find all Forms with 'id': (193, token) (found 1 results, but was looking for 2) error. Can anyone help me figure out what I'm doing wrong?
It seems I may have solved my own problem, posting it for Google and error checks:
def edit
#form = Form.find_by(token: params[:token], id: params[:id])
redirect_to pages_error_path if #form.sign.present?
end

Rails 4:register a person in my rails app through voice call verification

Can you please share that how can i make a outbound call to a person who want to register in my Rails 4 application and make a verification through his voice(in short i want to register a person in my rails app through voice call verification), Is that possible with twillo or suggest some other way to do it?
Please suggest the step by step or suggest some tutorial/doc/video
https://github.com/twilio/twilio-ruby
and
https://www.twilio.com/docs/quickstart/ruby/twiml
Call and ask for verification:
require 'twilio-ruby'
# build up a response
response = Twilio::TwiML::Response.new do |r|
r.Say 'hello there, enter verification code', :voice => 'alice'
r.Dial :callerId => '+14159992222' do |d|
r.Gather :numDigits => '5', :action => "/verify_code?id=#{id}", :method => 'get' do |g|
g.Say 'Verified thankyou.' if verified?
else
# hangup or set behaviour
end
end
end
end
def verify_code
#verification = Verification.where(verification_code: params[:id]).first
if #verification
#activate
else
# Handle errors
end
end
Finally in your model you'll need code that generates the verification code, and verifies if it is confirmed
class Verification < ActiveRecord::Base
before_create -> { self[:verification_code] = rand.to_s[2..5] }
end

Override default ActionMailer recipient at controller level

For demonstration purposes, I would like to override the Rails 4 ActionMailer recipient at controller level.
The following initializer does what I want, but when I want to change mail.to address, I need to recommit my code and restart the server.
# config/initializers/override_mail_recipient.rb
if Rails.env.production?
class OverrideMailRecipient
def self.delivering_email(mail)
mail.to = 'example#domain.com'
end
end
ActionMailer::Base.register_interceptor(OverrideMailRecipient)
end
What I would want is to be able to access a link like:
get '/default_mail_recipient/:email' => 'controller#default_mail_recipient', as: :mail_recipient
And then in a controller:
def default_mail_recipient
ActionMailer::Base.override_recipient = params[:email]
redirect_to :root, :notice => "Default mail recipient overridden"
end
I doubt that this will be possible, but maybe there's a Rails ninja out there that might help me or point me in another direction.

Error 404 when i use ActiveResource with Redmine

i have a problem with redmine. In fact, i've created a model who use ActiveResource :
require 'active_resource'
class New < ActiveResource::Base
#self.site = "http://localhost:3000/"
#self.format = :xml
#self.user = 'admin'
#self.password = 'admin'
class << self
attr_accessor :api_key
end
def save
prefix_options[:api_key] = self.class.api_key
super
end
end
New.site = 'http://localhost:3000'
New.api_key = '471bea6d1c4452b82b57287a281ff04392ae4118'
nw = New.new(:field_1 => 'value 1')
nw.save
# Retrieving news
news = New.find(:all)
puts news.first.title
#Retrieving an new
new = New.find(1)
puts new.description
puts new.author.name
# Creating an new
new = New.new(
:project_id => 1,
:author_id => 1,
:title => 'Annonce',
:summary => 'Annonce',
:description => 'Annonce'
)
if new.save
puts new.id
else
puts new.errors.full_messages
end
# Updating an 'new'
new = New.find(1)
new.title = 'NEW INFO '
new.save
# Deleting an new
new = New.find(1)
new.destroy
I'v an error 404 and i don't understand why:
/Users/bj/.rvm/gems/ruby-1.9.3-p429/gems/activeresource-4.0.0/lib/active_resource/connection.rb:144:in `handle_response': Failed. Response code =404. Response message = Not Found . (ActiveResource::ResourceNotFound)
PS: If i use api key or if i use self.site i've a 404 too !
Can you help me please? I tried many things but no every things doesn't works. Thanks for your answers !
The basic problem seems to be, that Redmine is not responding the way ActiveResource expects it. This could be due to changes in Rails 3, that where not properly reflected in Redmine yet.
The best option for you would maybe be dropping ActiveResource for something like httparty. You should also known, that Redmine currently only supports fetching all news (/news.json) and all news within a project (/projects/test_project/news.json). Neither fetching a single news directly nor creating or updating news is supported via the REST API.
Edit: The actual cause for the 404 in your code is due to the fact, that ActiveResource tried to POST to /news.json which is - as I have mentioned above - not supported by the REST API. If you remove the save call, you will run into another issue, where ActiveResource is not able to deserialize the response in New.find(:all). This lead me to think, that Redmine and ActiveResource are currently incompatible. Also the demo code in the Redmine wiki to create an issue, does not work for me.