Commit 10ffb4d17933c56b36909c256694cad94e6bfc61
Committed by
Rodrigo Souto
1 parent
11954a66
Exists in
api_private_token
and in
2 other branches
Added validation for profile types of target and requestor
Signed-off-by: André Bernardes <andrebsguedes@gmail.com> Signed-off-by: Eduardo Vital <vitaldu@gmail.com>
Showing
11 changed files
with
159 additions
and
5 deletions
Show diff stats
app/models/add_friend.rb
| ... | ... | @@ -14,6 +14,9 @@ class AddFriend < Task |
| 14 | 14 | alias :friend :target |
| 15 | 15 | alias :friend= :target= |
| 16 | 16 | |
| 17 | + validate :requestor_is_person | |
| 18 | + validate :target_is_person | |
| 19 | + | |
| 17 | 20 | after_create do |task| |
| 18 | 21 | TaskMailer.invitation_notification(task).deliver unless task.friend |
| 19 | 22 | remove_from_suggestion_list(task) |
| ... | ... | @@ -24,6 +27,18 @@ class AddFriend < Task |
| 24 | 27 | requestor.add_friend(target, group_for_person) |
| 25 | 28 | end |
| 26 | 29 | |
| 30 | + def requestor_is_person | |
| 31 | + unless requestor.person? | |
| 32 | + errors.add(:add_friend, N_('Requestor must be a person.')) | |
| 33 | + end | |
| 34 | + end | |
| 35 | + | |
| 36 | + def target_is_person | |
| 37 | + unless target.person? | |
| 38 | + errors.add(:add_friend, N_('Target must be a person.')) | |
| 39 | + end | |
| 40 | + end | |
| 41 | + | |
| 27 | 42 | def permission |
| 28 | 43 | :manage_friends |
| 29 | 44 | end | ... | ... |
app/models/add_member.rb
| ... | ... | @@ -2,6 +2,9 @@ class AddMember < Task |
| 2 | 2 | |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | 4 | |
| 5 | + validate :requestor_is_person | |
| 6 | + validate :target_is_organization | |
| 7 | + | |
| 5 | 8 | alias :person :requestor |
| 6 | 9 | alias :person= :requestor= |
| 7 | 10 | |
| ... | ... | @@ -55,4 +58,16 @@ class AddMember < Task |
| 55 | 58 | suggestion.disable if suggestion |
| 56 | 59 | end |
| 57 | 60 | |
| 61 | + def requestor_is_person | |
| 62 | + unless requestor.person? | |
| 63 | + errors.add(:add_member, N_('Requestor must be a person.')) | |
| 64 | + end | |
| 65 | + end | |
| 66 | + | |
| 67 | + def target_is_organization | |
| 68 | + unless target.organization? | |
| 69 | + errors.add(:add_member, N_('Target must be an organization.')) | |
| 70 | + end | |
| 71 | + end | |
| 72 | + | |
| 58 | 73 | end | ... | ... |
app/models/approve_article.rb
| 1 | 1 | class ApproveArticle < Task |
| 2 | 2 | validates_presence_of :requestor_id, :target_id |
| 3 | 3 | |
| 4 | + validate :requestor_is_person | |
| 5 | + validate :target_is_organization | |
| 6 | + validate :request_is_member_of_target | |
| 7 | + | |
| 4 | 8 | def article_title |
| 5 | 9 | article ? article.title : _('(The original text was removed)') |
| 6 | 10 | end |
| ... | ... | @@ -128,4 +132,21 @@ class ApproveArticle < Task |
| 128 | 132 | message |
| 129 | 133 | end |
| 130 | 134 | |
| 135 | + def requestor_is_person | |
| 136 | + unless requestor.person? | |
| 137 | + errors.add(:approve_article, N_('Requestor must be a person.')) | |
| 138 | + end | |
| 139 | + end | |
| 140 | + | |
| 141 | + def target_is_organization | |
| 142 | + unless target.organization? | |
| 143 | + errors.add(:approve_article, N_('Target must be an organization.')) | |
| 144 | + end | |
| 145 | + end | |
| 146 | + | |
| 147 | + def request_is_member_of_target | |
| 148 | + unless requestor.is_member_of?(target) | |
| 149 | + errors.add(:approve_article, N_('Requestor must be a member of target.')) | |
| 150 | + end | |
| 151 | + end | |
| 131 | 152 | end | ... | ... |
app/models/change_password.rb
| ... | ... | @@ -18,6 +18,8 @@ class ChangePassword < Task |
| 18 | 18 | |
| 19 | 19 | validates_presence_of :requestor |
| 20 | 20 | |
| 21 | + validate :requestor_is_person | |
| 22 | + | |
| 21 | 23 | ################################################### |
| 22 | 24 | # validations for updating a ChangePassword task |
| 23 | 25 | |
| ... | ... | @@ -72,4 +74,9 @@ class ChangePassword < Task |
| 72 | 74 | end |
| 73 | 75 | end |
| 74 | 76 | |
| 77 | + def requestor_is_person | |
| 78 | + unless requestor.person? | |
| 79 | + errors.add(:change_password, N_('Requestor must be a person.')) | |
| 80 | + end | |
| 81 | + end | |
| 75 | 82 | end | ... | ... |
app/models/create_community.rb
| ... | ... | @@ -3,6 +3,9 @@ class CreateCommunity < Task |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | 4 | validates_presence_of :name |
| 5 | 5 | |
| 6 | + validate :requestor_is_person | |
| 7 | + validate :target_is_environment | |
| 8 | + | |
| 6 | 9 | alias :environment :target |
| 7 | 10 | alias :environment= :target= |
| 8 | 11 | |
| ... | ... | @@ -92,4 +95,16 @@ class CreateCommunity < Task |
| 92 | 95 | _('Your request for registering the community "%{community}" was approved. You can access %{environment} now and start using your new community.') % { :community => self.name, :environment => self.environment } |
| 93 | 96 | end |
| 94 | 97 | |
| 98 | + def requestor_is_person | |
| 99 | + unless requestor.person? | |
| 100 | + errors.add(:create_community, N_('Requestor must be a person.')) | |
| 101 | + end | |
| 102 | + end | |
| 103 | + | |
| 104 | + def target_is_environment | |
| 105 | + unless target.class == Environment | |
| 106 | + errors.add(:create_community, N_('Target must be an environment.')) | |
| 107 | + end | |
| 108 | + end | |
| 109 | + | |
| 95 | 110 | end | ... | ... |
app/models/create_enterprise.rb
| ... | ... | @@ -27,6 +27,9 @@ class CreateEnterprise < Task |
| 27 | 27 | # checks for actual attributes |
| 28 | 28 | validates_presence_of :requestor_id, :target_id |
| 29 | 29 | |
| 30 | + validate :requestor_is_person | |
| 31 | + validate :target_is_environment | |
| 32 | + | |
| 30 | 33 | # checks for admins required attributes |
| 31 | 34 | DATA_FIELDS.each do |attribute| |
| 32 | 35 | validates_presence_of attribute, :if => lambda { |obj| obj.environment.required_enterprise_fields.include?(attribute) } |
| ... | ... | @@ -214,4 +217,16 @@ class CreateEnterprise < Task |
| 214 | 217 | :validate_enterprise |
| 215 | 218 | end |
| 216 | 219 | |
| 220 | + def requestor_is_person | |
| 221 | + unless requestor.person? | |
| 222 | + errors.add(:create_enterprise, N_('Requestor must be a person.')) | |
| 223 | + end | |
| 224 | + end | |
| 225 | + | |
| 226 | + def target_is_environment | |
| 227 | + unless target.class == Environment | |
| 228 | + errors.add(:create_enterprise, N_('Target must be an environment.')) | |
| 229 | + end | |
| 230 | + end | |
| 231 | + | |
| 217 | 232 | end | ... | ... |
app/models/email_activation.rb
| 1 | 1 | class EmailActivation < Task |
| 2 | 2 | |
| 3 | 3 | validates_presence_of :requestor_id, :target_id |
| 4 | + | |
| 5 | + validate :requestor_is_person | |
| 6 | + validate :target_is_environment | |
| 7 | + | |
| 4 | 8 | validate :already_requested, :on => :create |
| 5 | 9 | |
| 6 | 10 | alias :environment :target |
| 7 | 11 | alias :person :requestor |
| 8 | 12 | |
| 9 | 13 | def already_requested |
| 10 | - if !self.requestor.nil? && self.requestor.user.email_activation_pending? | |
| 11 | - self.errors.add(:base, _('You have already requested activation of your mailbox.')) | |
| 14 | + if self.requestor.person? | |
| 15 | + if !self.requestor.nil? && self.requestor.user.email_activation_pending? | |
| 16 | + self.errors.add(:base, _('You have already requested activation of your mailbox.')) | |
| 17 | + end | |
| 12 | 18 | end |
| 13 | 19 | end |
| 14 | 20 | |
| ... | ... | @@ -41,4 +47,16 @@ class EmailActivation < Task |
| 41 | 47 | false |
| 42 | 48 | end |
| 43 | 49 | |
| 50 | + def requestor_is_person | |
| 51 | + unless requestor.person? | |
| 52 | + errors.add(:email_activation, N_('Requestor must be a person.')) | |
| 53 | + end | |
| 54 | + end | |
| 55 | + | |
| 56 | + def target_is_environment | |
| 57 | + unless target.class == Environment | |
| 58 | + errors.add(:email_activation, N_('Target must be an environment.')) | |
| 59 | + end | |
| 60 | + end | |
| 61 | + | |
| 44 | 62 | end | ... | ... |
app/models/enterprise_activation.rb
| ... | ... | @@ -8,6 +8,9 @@ class EnterpriseActivation < Task |
| 8 | 8 | |
| 9 | 9 | validates_presence_of :enterprise |
| 10 | 10 | |
| 11 | + validate :requestor_is_person | |
| 12 | + validate :target_is_enterprise | |
| 13 | + | |
| 11 | 14 | def perform |
| 12 | 15 | self.enterprise.enable self.requestor |
| 13 | 16 | end |
| ... | ... | @@ -44,4 +47,16 @@ class EnterpriseActivation < Task |
| 44 | 47 | end |
| 45 | 48 | end |
| 46 | 49 | |
| 50 | + def requestor_is_person | |
| 51 | + unless requestor.person? | |
| 52 | + errors.add(:enterprise_activation, N_('Requestor must be a person.')) | |
| 53 | + end | |
| 54 | + end | |
| 55 | + | |
| 56 | + def target_is_enterprise | |
| 57 | + unless target.enterprise? | |
| 58 | + errors.add(:enterprise_activation, N_('Target must be an enterprise.')) | |
| 59 | + end | |
| 60 | + end | |
| 61 | + | |
| 47 | 62 | end | ... | ... |
app/models/invitation.rb
| ... | ... | @@ -6,6 +6,9 @@ class Invitation < Task |
| 6 | 6 | |
| 7 | 7 | validates_presence_of :target_id, :if => Proc.new{|invite| invite.friend_email.blank?} |
| 8 | 8 | |
| 9 | + validate :requestor_is_person | |
| 10 | + validate :target_is_person | |
| 11 | + | |
| 9 | 12 | validates_presence_of :friend_email, :if => Proc.new{|invite| invite.target_id.blank?} |
| 10 | 13 | validates_format_of :friend_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => Proc.new{|invite| invite.target_id.blank?} |
| 11 | 14 | |
| ... | ... | @@ -34,9 +37,11 @@ class Invitation < Task |
| 34 | 37 | end |
| 35 | 38 | |
| 36 | 39 | def not_invite_yourself |
| 37 | - email = friend ? friend.user.email : friend_email | |
| 38 | - if person && email && person.user.email == email | |
| 39 | - self.errors.add(:base, _("You can't invite youself")) | |
| 40 | + if friend.person? && person.person? | |
| 41 | + email = friend ? friend.user.email : friend_email | |
| 42 | + if person && email && person.user.email == email | |
| 43 | + self.errors.add(:base, _("You can't invite youself")) | |
| 44 | + end | |
| 40 | 45 | end |
| 41 | 46 | end |
| 42 | 47 | |
| ... | ... | @@ -139,4 +144,16 @@ class Invitation < Task |
| 139 | 144 | self.requestor.environment |
| 140 | 145 | end |
| 141 | 146 | |
| 147 | + def requestor_is_person | |
| 148 | + unless requestor.person? | |
| 149 | + errors.add(:invitation, N_('Requestor must be a person.')) | |
| 150 | + end | |
| 151 | + end | |
| 152 | + | |
| 153 | + def target_is_person | |
| 154 | + unless target.person? | |
| 155 | + errors.add(:invitation, N_('Target must be a person.')) | |
| 156 | + end | |
| 157 | + end | |
| 158 | + | |
| 142 | 159 | end | ... | ... |
app/models/moderate_user_registration.rb
| ... | ... | @@ -7,6 +7,8 @@ class ModerateUserRegistration < Task |
| 7 | 7 | |
| 8 | 8 | after_create :schedule_spam_checking |
| 9 | 9 | |
| 10 | + validate :target_is_environment | |
| 11 | + | |
| 10 | 12 | alias :environment :target |
| 11 | 13 | alias :environment= :target= |
| 12 | 14 | |
| ... | ... | @@ -56,4 +58,10 @@ class ModerateUserRegistration < Task |
| 56 | 58 | _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name } |
| 57 | 59 | end |
| 58 | 60 | |
| 61 | + def target_is_environment | |
| 62 | + unless environment.class == Environment | |
| 63 | + errors.add(:moderate_user_registration, N_('Target must be an environment.')) | |
| 64 | + end | |
| 65 | + end | |
| 66 | + | |
| 59 | 67 | end |
| 60 | 68 | \ No newline at end of file | ... | ... |
app/models/suggest_article.rb
| ... | ... | @@ -4,6 +4,8 @@ class SuggestArticle < Task |
| 4 | 4 | validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } |
| 5 | 5 | validates_associated :article_object |
| 6 | 6 | |
| 7 | + validate :target_is_organization | |
| 8 | + | |
| 7 | 9 | settings_items :email, :type => String |
| 8 | 10 | settings_items :name, :type => String |
| 9 | 11 | settings_items :ip_address, :type => String |
| ... | ... | @@ -92,4 +94,10 @@ class SuggestArticle < Task |
| 92 | 94 | def after_ham! |
| 93 | 95 | self.delay.marked_as_ham |
| 94 | 96 | end |
| 97 | + | |
| 98 | + def target_is_organization | |
| 99 | + unless target.organization? | |
| 100 | + errors.add(:suggest_article, N_('Target must be an organization.')) | |
| 101 | + end | |
| 102 | + end | |
| 95 | 103 | end | ... | ... |