mcms_authentication 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +17 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/images/background/page_bg.png +0 -0
  5. data/app/assets/images/background/text_field_background.png +0 -0
  6. data/app/assets/images/icons/accept.png +0 -0
  7. data/app/assets/images/icons/add.png +0 -0
  8. data/app/assets/images/icons/application_edit.png +0 -0
  9. data/app/assets/images/icons/cancel.png +0 -0
  10. data/app/assets/images/icons/delete.png +0 -0
  11. data/app/assets/images/icons/email_go.png +0 -0
  12. data/app/assets/images/rails.png +0 -0
  13. data/app/assets/javascripts/application.js +42 -0
  14. data/app/assets/javascripts/authentication_global.js +17 -0
  15. data/app/assets/stylesheets/application.css +33 -0
  16. data/app/assets/stylesheets/authentication_global.css +424 -0
  17. data/app/controllers/application_controller.rb +36 -0
  18. data/app/controllers/home_controller.rb +44 -0
  19. data/app/controllers/roles_controller.rb +375 -0
  20. data/app/controllers/users_controller.rb +202 -0
  21. data/app/models/ability.rb +82 -0
  22. data/app/models/existing_model.rb +24 -0
  23. data/app/models/plugin.rb +30 -0
  24. data/app/models/role.rb +70 -0
  25. data/app/models/roles_user.rb +33 -0
  26. data/app/models/user.rb +90 -0
  27. data/app/views/home/index.html.erb +18 -0
  28. data/app/views/layouts/users/_javascript.html.erb +3 -0
  29. data/app/views/layouts/users/_stylesheet.html.erb +3 -0
  30. data/app/views/layouts/users/devise.html.erb +40 -0
  31. data/app/views/layouts/users/home.html.erb +99 -0
  32. data/app/views/roles/_form.html.erb +240 -0
  33. data/app/views/roles/_form.js.erb +113 -0
  34. data/app/views/roles/edit.html.erb +26 -0
  35. data/app/views/roles/index.html.erb +73 -0
  36. data/app/views/roles/new.html.erb +25 -0
  37. data/app/views/users/_role.js.erb +47 -0
  38. data/app/views/users/confirmations/new.html.erb +29 -0
  39. data/app/views/users/edit.html.erb +131 -0
  40. data/app/views/users/index.html.erb +81 -0
  41. data/app/views/users/mailer/confirmation_instructions.html.erb +22 -0
  42. data/app/views/users/mailer/reset_password_instructions.html.erb +26 -0
  43. data/app/views/users/mailer/unlock_instructions.html.erb +24 -0
  44. data/app/views/users/new.html.erb +113 -0
  45. data/app/views/users/passwords/edit.html.erb +38 -0
  46. data/app/views/users/passwords/new.html.erb +32 -0
  47. data/app/views/users/sessions/new.html.erb +84 -0
  48. data/app/views/users/shared/_links.erb +39 -0
  49. data/app/views/users/unlocks/new.html.erb +25 -0
  50. data/config/initializers/constants.rb +30 -0
  51. data/config/initializers/devise.rb +217 -0
  52. data/config/locales/devise.en.yml +57 -0
  53. data/config/locales/en.yml +10 -0
  54. data/config/routes.rb +24 -0
  55. data/db/migrate/20120605112804_devise_create_users.rb +68 -0
  56. data/db/migrate/20120608104637_create_roles.rb +30 -0
  57. data/db/migrate/20120608140424_create_roles_users.rb +25 -0
  58. data/db/migrate/20120612050932_create_plugins.rb +14 -0
  59. data/db/migrate/20120625114340_create_existing_models.rb +9 -0
  60. data/db/migrate/20120711064709_add_username_to_users.rb +9 -0
  61. data/db/seeds.rb +29 -0
  62. data/lib/generators/mcms_authentication/USAGE +8 -0
  63. data/lib/generators/mcms_authentication/mcms_authentication_generator.rb +110 -0
  64. data/lib/generators/mcms_authentication/templates/asset_manager.rb +117 -0
  65. data/lib/generators/mcms_authentication/templates/models.rb +189 -0
  66. data/lib/mcms_authentication.rb +4 -0
  67. data/lib/mcms_authentication/engine.rb +20 -0
  68. data/lib/mcms_authentication/seeds.rb +14 -0
  69. data/lib/mcms_authentication/version.rb +3 -0
  70. data/lib/tasks/mcms_authentication_tasks.rake +4 -0
  71. data/test/dummy/README.rdoc +261 -0
  72. data/test/dummy/Rakefile +7 -0
  73. data/test/dummy/app/assets/javascripts/application.js +15 -0
  74. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  75. data/test/dummy/app/controllers/application_controller.rb +3 -0
  76. data/test/dummy/app/helpers/application_helper.rb +2 -0
  77. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  78. data/test/dummy/config.ru +4 -0
  79. data/test/dummy/config/application.rb +59 -0
  80. data/test/dummy/config/boot.rb +10 -0
  81. data/test/dummy/config/database.yml +25 -0
  82. data/test/dummy/config/environment.rb +5 -0
  83. data/test/dummy/config/environments/development.rb +37 -0
  84. data/test/dummy/config/environments/production.rb +67 -0
  85. data/test/dummy/config/environments/test.rb +37 -0
  86. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  87. data/test/dummy/config/initializers/inflections.rb +15 -0
  88. data/test/dummy/config/initializers/mime_types.rb +5 -0
  89. data/test/dummy/config/initializers/secret_token.rb +7 -0
  90. data/test/dummy/config/initializers/session_store.rb +8 -0
  91. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  92. data/test/dummy/config/locales/en.yml +5 -0
  93. data/test/dummy/config/routes.rb +58 -0
  94. data/test/dummy/public/404.html +26 -0
  95. data/test/dummy/public/422.html +26 -0
  96. data/test/dummy/public/500.html +25 -0
  97. data/test/dummy/public/favicon.ico +0 -0
  98. data/test/dummy/script/rails +6 -0
  99. data/test/fixtures/existing_models.yml +11 -0
  100. data/test/functional/home_controller_test.rb +7 -0
  101. data/test/integration/navigation_test.rb +10 -0
  102. data/test/mcms_authentication_test.rb +7 -0
  103. data/test/test_helper.rb +15 -0
  104. data/test/unit/existing_model_test.rb +7 -0
  105. data/test/unit/helpers/home_helper_test.rb +4 -0
  106. metadata +234 -0
@@ -0,0 +1,202 @@
1
+ =begin
2
+
3
+ @File Name :users_controller.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This controller is responsible for managing users
16
+
17
+ =end
18
+
19
+ class UsersController < Devise::RegistrationsController # inheriting Devise::Registrations controller to override the actions(CRUDS user)
20
+
21
+
22
+ prepend_before_filter :authenticate_user! # Obvious need of authetication before proceeding
23
+
24
+ load_and_authorize_resource # authorized users are allowed for the operations
25
+
26
+
27
+ # The following line seems to be redundant as we already said that every action requires authentication.
28
+ # But its actually not.We need to override devise behaviour to sign up non-logged in user.Here it needs a logged in user
29
+ prepend_before_filter :require_no_authentication, :except => [ :new,:index, :create,:edit,:destroy_user,:update ]
30
+
31
+ layout HOME_LAYOUT # using a layout
32
+
33
+ # @Params : No parameter
34
+ # @Returns : Nothing is returned GET mcms/users
35
+ # @Purpose : Showing all existing users
36
+
37
+ def index
38
+
39
+ @users = User.all # Selecting all users
40
+
41
+ respond_to do |f|
42
+
43
+ f.html # HTML response
44
+
45
+ f.json {render :json => @users} # JSON response
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+ # @Params : No parameter
53
+ # @Returns : Nothing is returned GET mcms/users/new
54
+ # @Purpose : Instantiating a new user
55
+
56
+ def new
57
+
58
+ @all_roles = Role.all # Roles needs to be there to select
59
+
60
+ super # else is upto devise
61
+
62
+ end
63
+
64
+
65
+ # @Params : No parameter
66
+ # @Returns : Nothing is returned POST mcms/users/
67
+ # @Purpose : Creating a new user
68
+
69
+ def create
70
+
71
+ build_resource # This helper is defined in devise which is building the resource (here user) so that we can have resource to be accessed here
72
+
73
+
74
+ @all_roles = Role.all # fetch all roles
75
+
76
+ if resource.save # saving the resource (here user)
77
+
78
+ if resource.active_for_authentication? # (we are not bother about active which is done after confirmation.In our case user is active upon registartion.No confirmation is needed. )
79
+
80
+ # saving the roles
81
+
82
+ @all_roles.each do |role|
83
+
84
+ tmp = role.id.to_s + VALUE
85
+
86
+
87
+
88
+ RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1"
89
+
90
+ end
91
+
92
+ flash[:notice] = t(:user_create_success,:user_email => resource.email ,:default => "#{resource.email} successfully created")
93
+
94
+ redirect_to mcms_users_path
95
+ else
96
+
97
+ @all_roles.each do |role|
98
+
99
+ tmp = role.id.to_s + VALUE
100
+
101
+ RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1"
102
+
103
+ end
104
+
105
+ set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
106
+
107
+ expire_session_data_after_sign_in!
108
+
109
+ respond_with resource, :location => after_inactive_sign_up_path_for(resource)
110
+
111
+ end
112
+
113
+ else
114
+
115
+ clean_up_passwords resource
116
+
117
+ respond_with resource
118
+
119
+ end
120
+
121
+ end
122
+
123
+ # @Params : No parameter
124
+ # @Returns : Nothing is returned POST mcms/users/1/edit
125
+ # @Purpose : Editing an existing user
126
+
127
+ def edit
128
+
129
+ @all_roles = Role.all
130
+
131
+ @user = User.find(params[:id]) # finding the user to be edited
132
+
133
+ respond_to do |f|
134
+ f.html #HTML response
135
+ f.json {render :json => @user} # JSON response
136
+ end
137
+
138
+ end
139
+
140
+ # @Params : No parameter
141
+ # @Returns : Nothing is returned POST mcms/users/1/edit
142
+ # @Purpose : Updating an existing user
143
+
144
+ def update
145
+
146
+ @user = User.find(params[:id]) # Finding the user to be updated
147
+
148
+ @all_roles = Role.all
149
+
150
+ # Following we are checking whether password field is kept blank ;so the old password stays.
151
+
152
+ params[:user].delete(:password) if params[:user][:password].blank?
153
+
154
+ params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
155
+
156
+ # Updating the attributes of users
157
+
158
+ if @user.update_attributes(params[:user])
159
+
160
+ # Assigning updated roles
161
+
162
+ @all_roles.each do |role|
163
+
164
+ tmp = role.id.to_s + VALUE
165
+
166
+ RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1" && RolesUser.find_by_role_id_and_user_id(Role.find_by_title(role.title).id,resource.id).nil?
167
+
168
+ end
169
+
170
+ flash[:notice] = t(:user_update_success,:user_email => @user.email,:default => "#{@user.email} successfully updated")
171
+
172
+ redirect_to mcms_users_path
173
+
174
+ else
175
+
176
+ render :action => 'edit'
177
+
178
+ end
179
+
180
+
181
+ end
182
+
183
+ # If the user needs to be destroyed
184
+
185
+ # @Params : No parameter
186
+ # @Returns : Nothing is returned DELETE mcms/users/1
187
+ # @Purpose : Destroying an existing user
188
+
189
+ def destroy_user
190
+
191
+ @user = User.find(params[:id]) # Find the user to be destroyed
192
+
193
+ @user.destroy # destroy it
194
+
195
+ flash[:notice] = t(:user_destroy_success,:user_email => @user.email,:default => "#{@user.email} successfully deleted")
196
+
197
+ redirect_to mcms_users_path #go to all users list
198
+
199
+ end
200
+
201
+
202
+ end
@@ -0,0 +1,82 @@
1
+ =begin
2
+
3
+ @File Name :ability.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This class is responsible for defining the abilities
16
+ of a particular user/current user
17
+
18
+ =end
19
+
20
+ class Ability
21
+
22
+ #including Cancan ability module
23
+
24
+ include CanCan::Ability
25
+
26
+ #including Models module from lib/mcms_authentication/models.rb
27
+
28
+ include Models
29
+
30
+
31
+ # @Params : No parameter
32
+ # @Returns : Nothing is returned
33
+ # @Purpose : Managing user roles
34
+
35
+ def initialize(user)
36
+
37
+ #fetching all the roles of current user
38
+ roles = Role.fetch_roles user
39
+
40
+
41
+ #defining abilities of current user
42
+
43
+ roles.each do |role|
44
+
45
+ can :manage, :all if role == "superuser"
46
+
47
+ # plugin access for a particular user role
48
+ @accessible_plugins = Plugin.find_all_by_role_id(Role.find_by_title(role).id)
49
+
50
+
51
+
52
+ @accessible_plugins.each do |plugin|
53
+
54
+
55
+
56
+ # models = get_relations plugin.role_module # getting associated models
57
+
58
+ models = ExistingModel.find_all_by_plugin_name(plugin.role_module.to_s)
59
+
60
+ models.each do |model|
61
+
62
+
63
+ # defining access control
64
+
65
+ can :read , model.model_name.constantize if plugin.role_read? # setting read authorization
66
+
67
+ can :create , model.model_name.constantize if plugin.role_create? # setting create authorization
68
+
69
+ can :update , model.model_name.constantize if plugin.role_update? # setting update authorization
70
+
71
+ can :destroy , model.model_name.constantize if plugin.role_destroy? # setting destroy authorization
72
+
73
+ can :manage , model.model_name.constantize if plugin.role_manage? # setting manage authorization
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,24 @@
1
+ =begin
2
+
3
+ @File Name :application_controller.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-25
10
+
11
+ @Date Modified :2012-06-25
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This model is storing models related to plugins
16
+
17
+ =end
18
+ class ExistingModel < ActiveRecord::Base
19
+
20
+ attr_accessible :plugin_name, :model_name
21
+
22
+ self.table_name = "mcms_existing_models"
23
+
24
+ end
@@ -0,0 +1,30 @@
1
+ =begin
2
+
3
+ @File Name :plugin.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This model is responsible for defining logic tracking
16
+ access control of a particular module for a particular
17
+ role
18
+
19
+ =end
20
+
21
+ class Plugin < ActiveRecord::Base
22
+
23
+
24
+ self.table_name = 'mcms_plugins' # setting my custome table name with this model (required to be declared here/not in application.rb as it is limited to my gem)
25
+
26
+ attr_accessible :id, :role_module , :role_read , :role_create , :role_update , :role_destroy , :role_manage , :role_id
27
+
28
+ belongs_to :role # it belongs to role
29
+
30
+ end
@@ -0,0 +1,70 @@
1
+ =begin
2
+
3
+ @File Name :role.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This model is required for logic set of role related data
16
+
17
+ =end
18
+
19
+ class Role < ActiveRecord::Base
20
+
21
+ self.table_name = 'mcms_roles' # setting my custome table name with this model (required to be declared here/not in application.rb as it is limited to my gem)
22
+
23
+ # it is associated with HABTM association to User model
24
+
25
+ attr_accessible :title
26
+
27
+ has_many :roles_users
28
+
29
+ has_many :users, :through => :roles_users
30
+
31
+ has_many :plugins
32
+
33
+ # each role needs an unique title
34
+
35
+ validates :title,:presence => true , :uniqueness => true
36
+
37
+
38
+ # @Params : Hash
39
+ # @Returns : Array
40
+ # @Purpose : This class method is responsible for fetching an user's roles
41
+
42
+ def self.fetch_roles user
43
+
44
+ roles = []
45
+
46
+ i = 0
47
+
48
+ if user.present?
49
+
50
+ if user.roles.present?
51
+
52
+ user.roles.each do |user_role|
53
+
54
+ roles[i] = user_role.title
55
+
56
+ i = i + 1
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+
65
+ roles # returning the roles array
66
+
67
+ end
68
+
69
+
70
+ end
@@ -0,0 +1,33 @@
1
+ =begin
2
+
3
+ @File Name :role.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :This model is required for logic set of role user relation
16
+
17
+ =end
18
+
19
+
20
+ class RolesUser < ActiveRecord::Base
21
+
22
+ # Join table/model for User and Role HABTM association
23
+
24
+ self.table_name = 'mcms_roles_users' # setting my custome table name with this model (required to be declared here/not in application.rb as it is limited to my gem)
25
+
26
+ attr_accessible :user_id , :role_id
27
+
28
+ belongs_to :user
29
+
30
+ belongs_to :role
31
+
32
+
33
+ end
@@ -0,0 +1,90 @@
1
+ =begin
2
+
3
+ @File Name :role.rb
4
+
5
+ @Company Name :Mindfire Solutions Pvt. Ltd.
6
+
7
+ @Creator Name :Indranil Mukherjee
8
+
9
+ @Date Created :2012-06-04
10
+
11
+ @Date Modified :2012-06-14
12
+
13
+ @Last Modification Details :Making it as mcms project standard
14
+
15
+ @Purpose :The User model is responsible for tracking all the users logics
16
+ rules to be imposed on users crud
17
+
18
+ =end
19
+
20
+ class User < ActiveRecord::Base
21
+
22
+ self.table_name = 'mcms_users' # setting my custome table name with this model (required to be declared here/not in application.rb as it is limited to my gem)
23
+
24
+
25
+ # The user model is added with the following devise functionalities
26
+
27
+ devise :database_authenticatable, :registerable,
28
+ :recoverable, :rememberable, :trackable, :validatable ,:token_authenticatable,
29
+ :lockable, :timeoutable
30
+
31
+ # every functionality is self explanatory
32
+
33
+ # for mass assignment we need the attributes to be accessible
34
+
35
+ attr_accessible :email, :first_name,:last_name,:password, :password_confirmation, :remember_me , :utf8, :_method, :authenticity_token, :user, :id, :commit, :action, :controller, :format
36
+
37
+
38
+
39
+ # @Params : Nothing
40
+ # @Returns : Nothing
41
+ # @Purpose : Cleaning up passwords
42
+
43
+ def clean_up_passwords
44
+
45
+ self.password = self.password_confirmation = nil
46
+
47
+ end
48
+
49
+ # HABTM association between Role and User model
50
+
51
+ has_many :roles_users
52
+
53
+ has_many :roles, :through => :roles_users
54
+
55
+
56
+ # @Params : String
57
+ # @Returns : Boolean
58
+ # @Purpose : whether the user has the passed role
59
+
60
+ def has_role? role_title
61
+
62
+ if self.roles.present?
63
+
64
+ self.roles.each do |role|
65
+
66
+ if role.title == role_title
67
+
68
+ return true
69
+
70
+ else
71
+
72
+ return false
73
+
74
+ end
75
+
76
+ end
77
+
78
+ else
79
+
80
+ return false
81
+
82
+ end
83
+
84
+ end
85
+
86
+
87
+
88
+
89
+
90
+ end