Merge with sub_roles

This commit is contained in:
chris2tof committed 2011-12-23 18:34:21 +08:00
1 parent fc591ee308
commit 62aaf79491
308 files changed
+2844 -17841

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
class AppAuth
include Mongoid::Document
include Mongoid::Timestamps
# after_save :update_block_list,:update_privilage_list
field :title
field :token
field :all ,type: Boolean,default: false
belongs_to :module_app
belongs_to :users
# belongs_to :users,as: :block_users, :inverse_of => :privilege_apps
has_and_belongs_to_many :blocked_users, :inverse_of => nil, :class_name => "User"
has_and_belongs_to_many :privilege_users, :inverse_of => nil, :class_name => "User"
has_and_belongs_to_many :roles
def add_user_to_black_list user
unless self.blocked_users.include?(user)
self.blocked_users << user
self.save!
else
false #should put error message for user existed in list already
end
end
def remove_user_from_black_list user
if self.blocked_users.include? user
self.blocked_users.delete user
self.save!
else
false #should put error message for user not existed in list
end
end
def add_user_to_privilege_list user
unless self.privilege_users.include? user
self.privilege_users << user
self.save!
else
false #should put error message for user existed in list already
end
end
def remove_user_from_privilege_list user
if self.privilege_users.include? user
self.privilege_users.delete user
self.save!
else
false #should put error message for user not existed in list
end
end
def auth_users
if self.all?
User.all.entries
else
ary= self.roles.collect do |role|
role.users
end
ary << self.privilege_users
ary.flatten!
end
end
def auth_users_after_block_list
auth_users - self.blocked_users
end
# protected
end
+34 -2
View File
@@ -24,6 +24,35 @@ class Design
after_save :procs_embedded_objects
# after_destroy :del_embedded_objects
# validate do
# errors.add('atttribute1', 'error for 1') if files_with_duplicate?
# errors.add('atttribute2', 'error for 1') if files_with_noname?
# end
def files_with_duplicate?
[self.javascripts, self.images,self.themes].each do |objects_hash|
ary = objects_hash.collect{ |k| k.file_filename}
ary.compact!
# debugger
return true if(ary.count!=ary.uniq.count)
end
false
end
def files_with_noname?
[self.javascripts, self.images,self.themes].each do |objects_hash|
objects_hash.each{ |k|
return true if(k.file_filename.nil? && !k.to_save?)
}
end
false
end
def new_files=(*attrs)
attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs
self.files=([items_ary, key])
end
end
def javascripts=(*attrs)
self.files = (attrs << 'javascripts')
end
@@ -36,7 +65,7 @@ class Design
self.files = (attrs << 'images')
end
# Update or create the attribute_model records
# Update or create the attribute records
def files=(attrs)
case attrs.last
when 'layout'
@@ -45,6 +74,7 @@ class Design
files = eval(attrs.last)
end
attrs[0].each do |a|
if a[:id].blank? && !a[:file].blank?
files.build(:file => a[:file], :to_save => true)
else
@@ -86,12 +116,14 @@ class Design
end
end
end
self.valid?
end
def process_object(object)
if object.to_save
if object.file_filename.nil?
new_object = self.send(object._type.downcase.pluralize).build(object)
new_object = self.send(object._type.downcase.pluralize).build(object.attributes)
new_object.file = object.file
object.destroy
new_object.to_save = false
new_object.save
+1 -1
View File
@@ -1,5 +1,5 @@
class Layout < DesignFile
include Parser
include ParserLayout
attr_reader :content
+2
View File
@@ -7,6 +7,8 @@ class I18nVariable
field :document_class, :type => String
field :parent_id, :type => BSON::ObjectId, :index => true
belongs_to :language_value, polymorphic: true
def method_missing(field)
self[field]
end
+5 -1
View File
@@ -2,7 +2,7 @@ class ModuleApp
include Mongoid::Document
include Mongoid::Timestamps
field :name_of_app
field :title
field :version
field :organization
field :author
@@ -10,4 +10,8 @@ class ModuleApp
field :update_info
field :create_date
field :app_pages ,type: Array
has_one :app_auth,dependent: :delete
end
+2
View File
@@ -1,10 +1,12 @@
class Page < Item
field :content
field :app_frontend_url
field :theme_id, :type => BSON::ObjectId, :default => nil
belongs_to :design
has_many :page_parts
belongs_to :module_app
embeds_many :custom_images, as: :design_image
+4
View File
@@ -9,4 +9,8 @@ class Purchase
field :downloaded, :type => Boolean
field :download_date, :type => Date
field :type
field :app_controller
has_many :app_auths
end
+26
View File
@@ -0,0 +1,26 @@
class Attribute
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
has_many :attribute_fields, :autosave => true, :dependent => :destroy
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :attribute_fields, :allow_destroy => true
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
def get_enabled_attribute_fields
self.attribute_fields.excludes('disabled' => true)
end
end
+37
View File
@@ -0,0 +1,37 @@
class AttributeField
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :markup
field :locale, :type => Boolean, :default => true
field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
belongs_to :attribute
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :attribute_values
# validates_uniqueness_of :key
# Convert the string list_options into an array
def select_list_options=(var)
self.list_options = var.gsub(' ', '').split(',')
end
# Convert the array list_options into a string
def select_list_options
self.list_options.to_a.join(', ')
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end
-62
View File
@@ -1,62 +0,0 @@
class AttributeModel
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embedded_in :user_attribute_model
validates_uniqueness_of :key
# Destroy the i18n_variable
def destroy_i18n_variable
self.i18n_variable.destroy rescue nil
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr)
else
# var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id}))
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Convert the string list_options into an array
def select_list_options=(var)
self.list_options = var.gsub(' ', '').split(',')
end
# Convert the array list_options into a string
def select_list_options
self.list_options.to_a.join(', ')
end
# Check if the attribute is set to be destroyed
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end
+6 -1
View File
@@ -5,6 +5,11 @@ class AttributeValue
field :key
belongs_to :user_attribute
belongs_to :attribute_field
belongs_to :user
def method_missing(field)
self[field]
end
end
+3
View File
@@ -0,0 +1,3 @@
class Info < Attribute
end
+29
View File
@@ -0,0 +1,29 @@
class Role
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
has_many :sub_roles, :autosave => true, :dependent => :destroy
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :users
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
accepts_nested_attributes_for :sub_roles, :allow_destroy => true
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
# Get an role from key
def self.get_role_from_key(key)
self.first(:conditions => {:key => key})
end
end
+11
View File
@@ -0,0 +1,11 @@
class SubRole < Attribute
belongs_to :role
has_and_belongs_to_many :users
# Get an sub_role from key
def self.get_sub_role_from_key(key)
self.first(:conditions => {:key => key})
end
end
+23 -39
View File
@@ -10,53 +10,37 @@ class User
field :admin, :type => Boolean, :default => true
field :active_role
has_many :user_attributes
has_many :user_roles
has_many :user_infos
has_many :attribute_values, :autosave => true, :dependent => :destroy
has_many :app_auths,as: :privilege_apps,:inverse_of => :privilege_lists
has_many :blocked_apps, :inverse_of => :blocked_users, :class_name => "AppAuth"
has_many :privilege_apps, :inverse_of => :privilege_users, :class_name => "AppAuth"
belongs_to :role
has_and_belongs_to_many :sub_roles
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
# Update or create the user_role records
def user_roles=(*attrs)
attrs[0].each do |roles|
if roles[:id].blank?
user_roles.build(roles)
else
user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s }
user_role.update_attributes(roles)
end
end
end
# Update or create the user_info records
def user_infos=(*attrs)
attrs[0].each do |infos|
if infos[:id].blank?
user_infos.build(infos)
else
user_info = user_infos.detect {|a| a.id.to_s == infos[:id].to_s }
user_info.update_attributes(infos)
end
end
end
# Get an user_info from model key
def get_info_from_model_key(key)
self.user_infos.detect {|a| a.key.to_s == key.to_s }
end
# Get an user_role from model key
def get_role_from_model_key(key)
self.user_roles.detect {|a| a.key.to_s == key.to_s }
def avb_apps
query = AppAuth.any_of({all: true},{privilege_user_ids: self.id},{roles: self.role.id}).excludes(blocked_user_ids: self.id)
end
def name
infos = self.user_infos.detect {|info| info.key.to_s.eql?('profile') }
if infos
first_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil
last_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('last_name') }[I18n.locale.to_s] rescue nil
info = Class::Info.first(:conditions => {:key => 'profile'})
if info
first_name = get_attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil
last_name = get_attribute_values.detect {|value| value.key.to_s.eql?('last_name') }[I18n.locale.to_s] rescue nil
return "#{first_name} #{last_name}"
else
return nil
end
end
def get_attribute_values
@attribute_values ||= self.attribute_values
end
def get_value_from_field_id(field_id)
values = get_attribute_values
value = values.detect {|value| value.attribute_field_id == field_id} rescue nil
value ? value : self.attribute_values.build
end
end
-21
View File
@@ -1,21 +0,0 @@
class UserAttribute
include Mongoid::Document
include Mongoid::Timestamps
field :key
has_many :attribute_values
# Update or create the attribute_value records
def attribute_values=(*args)
args[0].each do |value|
if value[:id].blank?
attribute_values.build(value)
else
attribute_value = attribute_values.detect {|a| a.id.to_s == value[:id].to_s }
attribute_value.update_attributes(value)
end
end
end
end
-74
View File
@@ -1,74 +0,0 @@
class UserAttributeModel
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embeds_many :attribute_models
after_update :destroy_attrs
# Update or create the attribute_model records
def attribute_models=(*attrs)
attrs[0].each do |attributes|
if attributes[:id].blank?
attribute_models.build(attributes)
else
attribute_model = attribute_models.detect {|a| a.id.to_s == attributes[:id].to_s }
attribute_model.update_attributes(attributes)
end
end
end
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_models.each do |attr|
attr.destroy_i18n_variable
end
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) rescue nil
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled.blank? ? false : self.disabled
end
def get_enabled_attribute_models
self.attribute_models.excludes('disabled' => true)
end
protected
# Destroy the i18n_variable for each attribute_models if marked to destroy
def destroy_attrs
attribute_models.each do |a|
if a.should_destroy?
a.destroy_i18n_variable
end
end
end
end
-5
View File
@@ -1,5 +0,0 @@
class UserInfo < UserAttribute
belongs_to :user
end
-3
View File
@@ -1,3 +0,0 @@
class UserInfoModel < UserAttributeModel
end
-5
View File
@@ -1,5 +0,0 @@
class UserRole < UserAttribute
belongs_to :user
end
-8
View File
@@ -1,8 +0,0 @@
class UserRoleModel < UserAttributeModel
# Get an user_role_model from key
def self.get_role_model_from_key(key)
self.first(:conditions => {:key => key})
end
end