loyal_passport 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +6 -0
- data/Rakefile +28 -0
- data/app/assets/javascripts/loyal_passport/admin/loyal_passport/assignments.js +2 -0
- data/app/assets/javascripts/loyal_passport/admin/loyal_passport/roles.js +2 -0
- data/app/assets/javascripts/loyal_passport/admin/users.js +2 -0
- data/app/assets/javascripts/loyal_passport/admin.js +0 -0
- data/app/assets/javascripts/loyal_passport/application.js +3 -0
- data/app/assets/javascripts/loyal_passport/login_colorbox.js +2 -0
- data/app/assets/stylesheets/loyal_passport/admin/loyal_passport/assignments.css +4 -0
- data/app/assets/stylesheets/loyal_passport/admin/loyal_passport/roles.css +4 -0
- data/app/assets/stylesheets/loyal_passport/admin/users.css +4 -0
- data/app/assets/stylesheets/loyal_passport/admin.css.scss +0 -0
- data/app/assets/stylesheets/loyal_passport/application.css.scss +278 -0
- data/app/assets/stylesheets/loyal_passport/login_colorbox.css.scss +0 -0
- data/app/assets/stylesheets/scaffold.css +56 -0
- data/app/controllers/loyal_passport/admin/loyal_passport/assignments_controller.rb +11 -0
- data/app/controllers/loyal_passport/admin/loyal_passport/roles_controller.rb +59 -0
- data/app/controllers/loyal_passport/admin/users_controller.rb +29 -0
- data/app/controllers/loyal_passport/application_controller.rb +9 -0
- data/app/controllers/loyal_passport/users/confirmations_controller.rb +6 -0
- data/app/controllers/loyal_passport/users/omniauth_callbacks_controller.rb +17 -0
- data/app/controllers/loyal_passport/users/passwords_controller.rb +8 -0
- data/app/controllers/loyal_passport/users/profile/informations_controller.rb +19 -0
- data/app/controllers/loyal_passport/users/registrations_controller.rb +71 -0
- data/app/controllers/loyal_passport/users/sessions_controller.rb +119 -0
- data/app/controllers/loyal_passport/users/unlocks_controller.rb +7 -0
- data/app/helpers/loyal_passport/admin/loyal_passport/assignments_helper.rb +5 -0
- data/app/helpers/loyal_passport/admin/loyal_passport/roles_helper.rb +5 -0
- data/app/helpers/loyal_passport/admin/users_helper.rb +5 -0
- data/app/helpers/loyal_passport/application_helper.rb +5 -0
- data/app/helpers/loyal_passport/users/confirmations_helper.rb +8 -0
- data/app/helpers/loyal_passport/users/omniauth_callbacks_helper.rb +8 -0
- data/app/helpers/loyal_passport/users/passwords_helper.rb +8 -0
- data/app/helpers/loyal_passport/users/registrations_helper.rb +8 -0
- data/app/helpers/loyal_passport/users/sessions_helper.rb +8 -0
- data/app/helpers/loyal_passport/users/unlocks_helper.rb +8 -0
- data/app/mailers/loyal_passport/users/mailer.rb +6 -0
- data/app/models/ability.rb +47 -0
- data/app/models/concerns/loyal_passport/homeworks_able.rb +36 -0
- data/app/models/loyal_passport/ability.rb +20 -0
- data/app/models/loyal_passport/assignment.rb +10 -0
- data/app/models/loyal_passport/homework.rb +42 -0
- data/app/models/loyal_passport/locker.rb +16 -0
- data/app/models/loyal_passport/oauth_bind.rb +6 -0
- data/app/models/loyal_passport/oauth_info.rb +36 -0
- data/app/models/loyal_passport/oauth_login.rb +9 -0
- data/app/models/loyal_passport/role.rb +25 -0
- data/app/models/session.rb +10 -0
- data/app/models/user.rb +99 -0
- data/app/views/layouts/loyal_passport/application.html.erb +42 -0
- data/app/views/loyal_passport/admin/loyal_passport/assignments/_assignment.html.erb +15 -0
- data/app/views/loyal_passport/admin/loyal_passport/roles/_form.html.erb +5 -0
- data/app/views/loyal_passport/admin/loyal_passport/roles/edit.html.erb +18 -0
- data/app/views/loyal_passport/admin/loyal_passport/roles/index.html.erb +25 -0
- data/app/views/loyal_passport/admin/loyal_passport/roles/new.html.erb +15 -0
- data/app/views/loyal_passport/admin/loyal_passport/roles/show.html.erb +59 -0
- data/app/views/loyal_passport/admin/users/_form.html.erb +27 -0
- data/app/views/loyal_passport/admin/users/edit.html.erb +14 -0
- data/app/views/loyal_passport/admin/users/index.html.erb +25 -0
- data/app/views/loyal_passport/admin/users/show.html.erb +27 -0
- data/app/views/loyal_passport/error/access_deny.html.erb +3 -0
- data/app/views/loyal_passport/users/confirmations/new.html.erb +19 -0
- data/app/views/loyal_passport/users/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/loyal_passport/users/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/loyal_passport/users/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/loyal_passport/users/passwords/edit.html.erb +23 -0
- data/app/views/loyal_passport/users/passwords/new.html.erb +19 -0
- data/app/views/loyal_passport/users/profile/informations/index.html.erb +35 -0
- data/app/views/loyal_passport/users/registrations/edit.html.erb +47 -0
- data/app/views/loyal_passport/users/registrations/new.html.erb +40 -0
- data/app/views/loyal_passport/users/sessions/new.html.erb +26 -0
- data/app/views/loyal_passport/users/shared/_flashes.html.erb +8 -0
- data/app/views/loyal_passport/users/shared/_footer.html.erb +6 -0
- data/app/views/loyal_passport/users/shared/_links.erb +41 -0
- data/app/views/loyal_passport/users/shared/_nav.html.erb +1 -0
- data/app/views/loyal_passport/users/shared/_status.html.erb +18 -0
- data/app/views/loyal_passport/users/unlocks/new.html.erb +20 -0
- data/config/initializers/loyal_passport.rb +265 -0
- data/config/locales/loyal_passport/en/devise.en.yml +63 -0
- data/config/locales/loyal_passport/en/loyal_passport.en.yml +20 -0
- data/config/locales/loyal_passport/en/views/mailer.en.yml +15 -0
- data/config/locales/loyal_passport/zh-CN/devise.zh-CN.yml +65 -0
- data/config/locales/loyal_passport/zh-CN/loyal_passport.zh-CN.yml +47 -0
- data/config/locales/loyal_passport/zh-CN/models/loyal_passport/assignment.zh-CN.yml +24 -0
- data/config/locales/loyal_passport/zh-CN/user.yml +82 -0
- data/config/locales/loyal_passport/zh-CN/views/mailer.zh-CN.yml +14 -0
- data/config/routes.rb +36 -0
- data/db/migrate/20121123023890_create_users.rb +80 -0
- data/db/migrate/20121123025990_create_loyal_passport_roles.rb +24 -0
- data/db/migrate/20121123026990_create_loyal_passport_assignments.rb +24 -0
- data/db/migrate/20121125104021_add_sessions_table.rb +13 -0
- data/db/migrate/20130323050903_create_loyal_passport_lockers.rb +19 -0
- data/db/migrate/20130526062222_create_loyal_passport_homeworks.rb +12 -0
- data/db/migrate/20130625053301_create_loyal_passport_oauth_infos.rb +40 -0
- data/db/migrate/20130625053334_create_loyal_passport_oauth_binds.rb +17 -0
- data/db/migrate/20130625053410_create_loyal_passport_oauth_logins.rb +15 -0
- data/db/migrate/20130625055117_add_gender_to_users.rb +7 -0
- data/lib/loyal_passport/acts/acts_as_author_able.rb +26 -0
- data/lib/loyal_passport/acts/acts_as_locker_able.rb +26 -0
- data/lib/loyal_passport/acts.rb +12 -0
- data/lib/loyal_passport/config.rb +158 -0
- data/lib/loyal_passport/controllers/controller_extends.rb +28 -0
- data/lib/loyal_passport/controllers/custom_failure_app.rb +238 -0
- data/lib/loyal_passport/controllers/devise_extends.rb +26 -0
- data/lib/loyal_passport/controllers/passport_basic.rb +65 -0
- data/lib/loyal_passport/controllers/users_basic.rb +33 -0
- data/lib/loyal_passport/devise_helper.rb +22 -0
- data/lib/loyal_passport/engine.rb +11 -0
- data/lib/loyal_passport/user_parameter_sanitizer.rb +25 -0
- data/lib/loyal_passport/utils/array_util.rb +16 -0
- data/lib/loyal_passport/utils/data_util.rb +181 -0
- data/lib/loyal_passport/utils.rb +5 -0
- data/lib/loyal_passport/version.rb +4 -0
- data/lib/loyal_passport.rb +30 -0
- data/lib/tasks/loyal_passport_tasks.rake +4 -0
- metadata +209 -0
@@ -0,0 +1,119 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module LoyalPassport
|
3
|
+
class Users::SessionsController < Devise::SessionsController
|
4
|
+
include ::LoyalPassport::Controllers::UsersBasic
|
5
|
+
|
6
|
+
def new
|
7
|
+
self.resource = resource_class.new(sign_in_params)
|
8
|
+
self.resource.remember_me ||= true
|
9
|
+
clean_up_passwords(resource)
|
10
|
+
respond_with(resource, serialize_options(resource))
|
11
|
+
end
|
12
|
+
|
13
|
+
def create
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
|
20
|
+
# prepend_before_filter :allow_params_authentication!, :only => :create
|
21
|
+
# prepend_before_filter { request.env["devise.skip_timeout"] = true }
|
22
|
+
#
|
23
|
+
# # GET /resource/sign_in
|
24
|
+
# def new
|
25
|
+
# self.resource = resource_class.new(sign_in_params)
|
26
|
+
# clean_up_passwords(resource)
|
27
|
+
# respond_with(resource, serialize_options(resource))
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# # POST /resource/sign_in
|
31
|
+
# def create
|
32
|
+
# self.resource = warden.authenticate!(auth_options)
|
33
|
+
# set_flash_message(:notice, :signed_in) if is_navigational_format?
|
34
|
+
# sign_in(resource_name, resource)
|
35
|
+
# respond_with resource, :location => after_sign_in_path_for(resource)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# # DELETE /resource/sign_out
|
39
|
+
# def destroy
|
40
|
+
# redirect_path = after_sign_out_path_for(resource_name)
|
41
|
+
# signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
|
42
|
+
# set_flash_message :notice, :signed_out if signed_out && is_navigational_format?
|
43
|
+
#
|
44
|
+
# # We actually need to hardcode this as Rails default responder doesn't
|
45
|
+
# # support returning empty response on GET request
|
46
|
+
# respond_to do |format|
|
47
|
+
# format.all { head :no_content }
|
48
|
+
# format.any(*navigational_formats) { redirect_to redirect_path }
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# protected
|
53
|
+
#
|
54
|
+
# def sign_in_params
|
55
|
+
# devise_parameter_sanitizer.for(:sign_in)
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# def serialize_options(resource)
|
59
|
+
# methods = resource_class.authentication_keys.dup
|
60
|
+
# methods = methods.keys if methods.is_a?(Hash)
|
61
|
+
# methods << :password if resource.respond_to?(:password)
|
62
|
+
# { :methods => methods, :only => [:password] }
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# def auth_options
|
66
|
+
# { :scope => resource_name, :recall => "#{controller_path}#new" }
|
67
|
+
# end
|
68
|
+
|
69
|
+
|
70
|
+
# class Devise::SessionsController < DeviseController
|
71
|
+
# prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
|
72
|
+
# prepend_before_filter :allow_params_authentication!, :only => :create
|
73
|
+
# prepend_before_filter { request.env["devise.skip_timeout"] = true }
|
74
|
+
#
|
75
|
+
# # GET /resource/sign_in
|
76
|
+
# def new
|
77
|
+
# resource = build_resource(nil, :unsafe => true)
|
78
|
+
# clean_up_passwords(resource)
|
79
|
+
# respond_with(resource, serialize_options(resource))
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # POST /resource/sign_in
|
83
|
+
# def create
|
84
|
+
# resource = warden.authenticate!(auth_options)
|
85
|
+
# set_flash_message(:notice, :signed_in) if is_navigational_format?
|
86
|
+
# sign_in(resource_name, resource)
|
87
|
+
# respond_with resource, :location => after_sign_in_path_for(resource)
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# # DELETE /resource/sign_out
|
91
|
+
# def destroy
|
92
|
+
# redirect_path = after_sign_out_path_for(resource_name)
|
93
|
+
# signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
|
94
|
+
# set_flash_message :notice, :signed_out if signed_out
|
95
|
+
#
|
96
|
+
# # We actually need to hardcode this as Rails default responder doesn't
|
97
|
+
# # support returning empty response on GET request
|
98
|
+
# respond_to do |format|
|
99
|
+
# format.any(*navigational_formats) { redirect_to redirect_path }
|
100
|
+
# format.all do
|
101
|
+
# head :no_content
|
102
|
+
# end
|
103
|
+
# end
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# protected
|
107
|
+
#
|
108
|
+
# def serialize_options(resource)
|
109
|
+
# methods = resource_class.authentication_keys.dup
|
110
|
+
# methods = methods.keys if methods.is_a?(Hash)
|
111
|
+
# methods << :password if resource.respond_to?(:password)
|
112
|
+
# { :methods => methods, :only => [:password] }
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# def auth_options
|
116
|
+
# { :scope => resource_name, :recall => "#{controller_path}#new" }
|
117
|
+
# end
|
118
|
+
# end
|
119
|
+
#
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class Ability
|
3
|
+
include ::CanCan::Ability
|
4
|
+
|
5
|
+
def initialize(current_user)
|
6
|
+
# if user.roles?(:admin)
|
7
|
+
# can :manage, :all
|
8
|
+
# end
|
9
|
+
|
10
|
+
current_user ||= ::User.new # guest user (not logged in)
|
11
|
+
|
12
|
+
# 如果用户是超级管理员
|
13
|
+
if current_user && current_user.super_admin?
|
14
|
+
can :manage, :all
|
15
|
+
end
|
16
|
+
|
17
|
+
# 执行
|
18
|
+
LoyalPassport.config.cancan_abilities.each do |ability|
|
19
|
+
if ability.is_a?(String)
|
20
|
+
ability.constantize.new self, current_user
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Define abilities for the passed in user here. For example:
|
25
|
+
#
|
26
|
+
# user ||= User.new # guest user (not logged in)
|
27
|
+
# if user.admin?
|
28
|
+
# can :manage, :all
|
29
|
+
# else
|
30
|
+
# can :read, :all
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# The first argument to `can` is the action you are giving the user permission to do.
|
34
|
+
# If you pass :manage it will apply to every action. Other common actions here are
|
35
|
+
# :read, :create, :update and :destroy.
|
36
|
+
#
|
37
|
+
# The second argument is the resource the user can perform the action on. If you pass
|
38
|
+
# :all it will apply to every resource. Otherwise pass a Ruby class of the resource.
|
39
|
+
#
|
40
|
+
# The third argument is an optional hash of conditions to further filter the objects.
|
41
|
+
# For example, here the user can only update published articles.
|
42
|
+
#
|
43
|
+
# can :update, Article, :published => true
|
44
|
+
#
|
45
|
+
# See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# include Concerns::LoyalPassport::HomeworksAble
|
3
|
+
module Concerns
|
4
|
+
module LoyalPassport
|
5
|
+
module HomeworksAble
|
6
|
+
def self.included base
|
7
|
+
base.class_eval do
|
8
|
+
include InstanceMethods
|
9
|
+
|
10
|
+
extend ::LoyalCore::Memoist
|
11
|
+
|
12
|
+
memoize :homework?, :unhomework?, :homeworks_array
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
|
18
|
+
def homework? job_name, name
|
19
|
+
!(unhomework? job_name, name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def homeworks_array
|
23
|
+
self.homeworks.to_a
|
24
|
+
end
|
25
|
+
|
26
|
+
def unhomework? job_name, name
|
27
|
+
self.homeworks_array.find { |homework|
|
28
|
+
homework.job_name == job_name.to_s &&
|
29
|
+
homework.name == name.to_s
|
30
|
+
}.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class LoyalPassport::Ability
|
3
|
+
def initialize ability, current_user
|
4
|
+
# 屏蔽用户
|
5
|
+
ability.can [:review], [::User] do |user|
|
6
|
+
# 不是超级用户 当前用户有相应权限 被管理的用户有相应的权限
|
7
|
+
user.not_super_admin? && current_user.homework?(:review, :user) && user.unhomework?(:review, :user)
|
8
|
+
end
|
9
|
+
|
10
|
+
ability.can [:destroy, :block], [::User] do |user|
|
11
|
+
ability.can?(:review, user)
|
12
|
+
end
|
13
|
+
|
14
|
+
# 能更新用户的信息
|
15
|
+
ability.can [:update], [::User] do |user|
|
16
|
+
(current_user.id == user.id) || ability.can?(:review, user)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class LoyalPassport::Assignment < ActiveRecord::Base
|
3
|
+
attr_accessible :user_id, :role_id
|
4
|
+
|
5
|
+
belongs_to :role, class_name: 'LoyalPassport::Role'
|
6
|
+
belongs_to :user, class_name: 'User'
|
7
|
+
|
8
|
+
validates :role, :user, :presence => true
|
9
|
+
validates_uniqueness_of :user_id, :scope => [:role_id]
|
10
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module LoyalPassport
|
3
|
+
class Homework < ActiveRecord::Base
|
4
|
+
attr_accessible :name, :job_name
|
5
|
+
|
6
|
+
belongs_to :role, :class_name => "LoyalPassport::Role"
|
7
|
+
validates_uniqueness_of :role_id, :scope => [:name, :job_name]
|
8
|
+
validates_presence_of :role_id, :name, :job_name
|
9
|
+
|
10
|
+
self.acts_as_tiny_cached
|
11
|
+
|
12
|
+
validate do |r|
|
13
|
+
r.errors.add :job_name, '没有配置' unless r.authoritie_job_config?
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.authoritie_configs
|
17
|
+
::LoyalPassport.config.authoritie_configs
|
18
|
+
end
|
19
|
+
|
20
|
+
def authoritie_job_config
|
21
|
+
@authoritie_job_config ||= (
|
22
|
+
config = (
|
23
|
+
(
|
24
|
+
self.class.authoritie_configs[self.name] || {}
|
25
|
+
)[:jobs] || {}
|
26
|
+
)[self.job_name]
|
27
|
+
|
28
|
+
case config
|
29
|
+
when Hash
|
30
|
+
config
|
31
|
+
when String
|
32
|
+
{:desc => config}
|
33
|
+
end
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def authoritie_job_config?
|
38
|
+
self.authoritie_job_config.is_a?(Hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module LoyalPassport
|
3
|
+
class Locker < ActiveRecord::Base
|
4
|
+
# attr_accessible :title, :body
|
5
|
+
|
6
|
+
LOCKER_ABLE_LEVEL_CONFIG = ::LoyalCore::ConfigUtil.new(
|
7
|
+
{ :key => :nothing, :value => 0, :desc => '不能操作' },
|
8
|
+
{ :key => :read, :value => 2**0, :desc => '读' },
|
9
|
+
{ :key => :write, :value => 2**1, :desc => '写' },
|
10
|
+
{ :key => :delete, :value => 2**2, :desc => '删' }
|
11
|
+
)
|
12
|
+
|
13
|
+
self.loyal_core_acts_as_bit_able :level, :config => LOCKER_ABLE_LEVEL_CONFIG
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module LoyalPassport
|
3
|
+
class OauthInfo < ActiveRecord::Base
|
4
|
+
# attr_accessible :title, :body
|
5
|
+
STRATEGY_MAP_CONFIG = {
|
6
|
+
:github => { :value => 1, :name => 'Github' },
|
7
|
+
:qq_connect => { :value => 2, :name => "QQ"}
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
STRATEGY_VALUES = STRATEGY_MAP_CONFIG.values.map do |_config|
|
11
|
+
_config[:value]
|
12
|
+
end.freeze
|
13
|
+
|
14
|
+
validates_inclusion_of :strategy_flag, :in => STRATEGY_VALUES
|
15
|
+
|
16
|
+
def self.save_with_callback_info strategy_name, strategy_id, info={}
|
17
|
+
strategy_flag = STRATEGY_MAP_CONFIG[strategy_name.to_sym][:value]
|
18
|
+
|
19
|
+
oauth_info_scope = self.class.where(
|
20
|
+
:strategy_flag => strategy_flag,
|
21
|
+
:strategy_id => strategy_id
|
22
|
+
)
|
23
|
+
|
24
|
+
oauth_info = oauth_info_scope.first || oauth_info_scope.new
|
25
|
+
|
26
|
+
oauth_info.access_token = info['credentials']['token']
|
27
|
+
oauth_info.expired_at = Time.at(info['credentials']['expires_at'].to_i)
|
28
|
+
oauth_info.origin_avatar_url = info['info']['avatar']
|
29
|
+
oauth_info.gender = info['info']['gender']
|
30
|
+
oauth_info.nick_name = info['info']['nick_name']
|
31
|
+
|
32
|
+
oauth_info.save
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class LoyalPassport::Role < ActiveRecord::Base
|
3
|
+
include ::Concerns::LoyalPassport::HomeworksAble
|
4
|
+
|
5
|
+
attr_accessible :permalink, :name, :instroduction, :description, :user_ids
|
6
|
+
|
7
|
+
self.acts_as_tiny_cached
|
8
|
+
|
9
|
+
# 角色分类 ###################
|
10
|
+
has_many :assignments, class_name: 'LoyalPassport::Assignment',
|
11
|
+
:foreign_key => :role_id, :dependent => :destroy
|
12
|
+
|
13
|
+
has_many :users, :class_name => 'User',
|
14
|
+
:through => :assignments
|
15
|
+
|
16
|
+
has_many :homeworks, :class_name => 'LoyalPassport::Homework',
|
17
|
+
:dependent => :destroy
|
18
|
+
|
19
|
+
self.strip_whitespace_before_validation :permalink, :name
|
20
|
+
|
21
|
+
# 不能为空,且唯一
|
22
|
+
validates :permalink, :presence => true, :uniqueness => true
|
23
|
+
validates :name, :presence => true
|
24
|
+
|
25
|
+
end
|
data/app/models/user.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class User < ActiveRecord::Base
|
3
|
+
include ::Concerns::LoyalPassport::HomeworksAble
|
4
|
+
|
5
|
+
# PERMALINK_REGEXP = /^[A-Za-z0-9\_]+$/.freeze
|
6
|
+
PERMALINK_REGEXP = /^[A-Za-z0-9]+$/.freeze
|
7
|
+
|
8
|
+
# attr_accessible :title, :body
|
9
|
+
attr_accessible :nick_name, :true_name, :role_ids, :permalink
|
10
|
+
|
11
|
+
self.apply_simple_captcha :message => I18n.t('activerecord.errors.models.user.attributes.captcha.message')
|
12
|
+
|
13
|
+
self.acts_as_tiny_cached
|
14
|
+
|
15
|
+
# 软删除
|
16
|
+
self.acts_as_paranoid
|
17
|
+
|
18
|
+
# Include default devise modules. Others available are:
|
19
|
+
# :token_authenticatable, :confirmable,
|
20
|
+
# :lockable, :timeoutable and :omniauthable
|
21
|
+
devise :database_authenticatable,
|
22
|
+
:registerable,
|
23
|
+
:recoverable,
|
24
|
+
:rememberable,
|
25
|
+
:trackable,
|
26
|
+
:validatable,
|
27
|
+
:token_authenticatable,
|
28
|
+
:confirmable,
|
29
|
+
:lockable,
|
30
|
+
:timeoutable,
|
31
|
+
:omniauthable
|
32
|
+
|
33
|
+
# Setup accessible (or protected) attributes for your model
|
34
|
+
attr_accessible :email, :password, :password_confirmation, :remember_me
|
35
|
+
|
36
|
+
validates :nick_name, :presence => true, :uniqueness => true,
|
37
|
+
:length => {:minimum => 2, :maximum => 12}
|
38
|
+
|
39
|
+
# 岗位分配 ##################
|
40
|
+
has_many :assignments, class_name: 'LoyalPassport::Assignment',
|
41
|
+
foreign_key: :user_id
|
42
|
+
|
43
|
+
has_many :roles, class_name: 'LoyalPassport::Role',
|
44
|
+
through: :assignments
|
45
|
+
|
46
|
+
has_many :homeworks, :class_name => 'LoyalPassport::Homework',
|
47
|
+
:through => :roles
|
48
|
+
|
49
|
+
# 去除空格
|
50
|
+
self.strip_whitespace_before_validation :email, :nick_name, :true_name,
|
51
|
+
:mobile_number, :permalink
|
52
|
+
|
53
|
+
validates_format_of :permalink, :with => PERMALINK_REGEXP, :multiline => true
|
54
|
+
validates_length_of :permalink, :minimum => 3, :maximum => 12
|
55
|
+
|
56
|
+
# 头像
|
57
|
+
self.loyal_core_acts_as_has_avatar :avatar
|
58
|
+
|
59
|
+
def ability
|
60
|
+
@ability ||= ::Ability.new(self)
|
61
|
+
end
|
62
|
+
|
63
|
+
# eg: some_user.can? :update, @article
|
64
|
+
delegate :can?, :cannot?, :to => :ability
|
65
|
+
|
66
|
+
def roles?(*args)
|
67
|
+
options = args.extract_options!
|
68
|
+
|
69
|
+
self.super_admin? || self.roles.exists?({:permalink => args})
|
70
|
+
end
|
71
|
+
|
72
|
+
def super_admin?
|
73
|
+
['happy'].include?(self.nick_name)
|
74
|
+
end
|
75
|
+
|
76
|
+
def not_super_admin?
|
77
|
+
!super_admin?
|
78
|
+
end
|
79
|
+
|
80
|
+
extend ::LoyalCore::Memoist
|
81
|
+
|
82
|
+
memoize :not_super_admin?, :super_admin?
|
83
|
+
|
84
|
+
class << self
|
85
|
+
# 能注销帐号吗?
|
86
|
+
def can_cancel_account?
|
87
|
+
false && ::LoyalPassport.config.logics[:open_account_cancel?]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def can_cancel_account?
|
92
|
+
self.class.can_cancel_account? && !(
|
93
|
+
self.super_admin? # 不是超级管理员
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
5
|
+
<title><%= I18n.t('loyal_passport.name') %></title>
|
6
|
+
<%= stylesheet_link_tag "loyal_passport/application", :media => "all" %>
|
7
|
+
<%= javascript_include_tag "loyal_passport/application" %>
|
8
|
+
<%= csrf_meta_tags %>
|
9
|
+
</head>
|
10
|
+
<body>
|
11
|
+
|
12
|
+
<div id='nav' class='clearfix'>
|
13
|
+
<div class='nav-inner wrapper'>
|
14
|
+
<div class='status'>
|
15
|
+
<%= render :partial => 'loyal_passport/users/shared/status' %>
|
16
|
+
</div>
|
17
|
+
</div>
|
18
|
+
</div>
|
19
|
+
</div>
|
20
|
+
|
21
|
+
<div id='container' class='clearfix'>
|
22
|
+
<div class='content-block wrapper clearfix'>
|
23
|
+
<% if user_signed_in? %>
|
24
|
+
<%= render :partial => 'loyal_passport/users/shared/nav' %>
|
25
|
+
<% end %>
|
26
|
+
|
27
|
+
<%= render :partial => 'loyal_passport/users/shared/flashes' %>
|
28
|
+
|
29
|
+
<div class='main-content clearfix'>
|
30
|
+
<%= yield %>
|
31
|
+
</div>
|
32
|
+
|
33
|
+
<div class='side-content clearfix'>
|
34
|
+
<%= yield :side %>
|
35
|
+
</div>
|
36
|
+
</div>
|
37
|
+
</div>
|
38
|
+
|
39
|
+
<%= render :partial => 'loyal_passport/users/shared/footer' %>
|
40
|
+
|
41
|
+
</body>
|
42
|
+
</html>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<li>
|
2
|
+
<% if assignment.user %>
|
3
|
+
<%= link_to "#{assignment.user.nick_name}", loyal_passport_app.admin_user_url(
|
4
|
+
:id => assignment.user.id
|
5
|
+
) %>
|
6
|
+
<% end %>
|
7
|
+
(
|
8
|
+
<%= link_to '删除', loyal_passport_app.admin_loyal_passport_assignment_url(
|
9
|
+
:return_to => request.url,
|
10
|
+
:id => assignment.id
|
11
|
+
), :method => :delete, :data => {
|
12
|
+
:confirm => '你确定要删除吗?'
|
13
|
+
} %>
|
14
|
+
)
|
15
|
+
</li>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<h1>编辑角色</h1>
|
2
|
+
|
3
|
+
<%= form_for(
|
4
|
+
@loyal_passport_role,
|
5
|
+
:url => loyal_passport_app.admin_loyal_passport_role_url(:id => @loyal_passport_role.id),
|
6
|
+
:builder => ::LoyalCore::ActionView::LabeledBuilder,
|
7
|
+
:as => :loyal_passport_role
|
8
|
+
) do |f| %>
|
9
|
+
<%= render :partial => 'form', :locals => { :f => f } %>
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<div>
|
13
|
+
<%= link_to "返回列表", loyal_passport_app.admin_loyal_passport_roles_url %>
|
14
|
+
<%= link_to "取消", loyal_passport_app.admin_loyal_passport_role_url(
|
15
|
+
:id => @loyal_passport_role.id
|
16
|
+
) %>
|
17
|
+
</div>
|
18
|
+
|