diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb new file mode 100644 index 000000000..8e49a49f6 --- /dev/null +++ b/app/controllers/account_controller.rb @@ -0,0 +1,12 @@ +class AccountController < ApplicationController + # GET /account + # + def show + end + + # GET /account/edit + # + def edit + @user = current_user + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2b934011e..923b2a603 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -47,7 +47,7 @@ def store_location def after_sign_in_path_for(user) if user.members.present? - users_path + members_path else page_path("about") end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index d257c39e4..6da7e5d10 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,5 @@ class HomeController < ApplicationController def index - redirect_to :users if current_user + redirect_to :members if current_user end end diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index c88ea52df..3fc377730 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -1,11 +1,36 @@ class MembersController < ApplicationController before_filter :authenticate_user! + # GET /members + # + def index + context = current_organization + .members + .includes(:account, :user) + context = context.where(active: true) unless (admin? || superadmin?) + + @memberships = context + end + + # GET /members/:member_uid + # + def show + find_member + @user = @member.user + @movements = @member + .movements + .order('created_at DESC') + .page(params[:page]) + .per(10) + end + + # DELETE /members/:member_uid + # def destroy find_member toggle_active_posts @member.destroy - redirect_to users_path + redirect_to members_path end def toggle_manager @@ -31,14 +56,54 @@ def toggle_active end end + # TODO: move to service and probably different controller + def give_time + find_member + @destination = @member.account.id + @source = find_transfer_source + @offer = find_transfer_offer + @transfer = Transfer.new( + source: @source, + destination: @destination, + post: @offer + ) + @sources = find_transfer_sources_for_admin + end + private + # TODO: rely on organization scope instead of current_organization def find_member - @member ||= current_organization.members.find(params[:id]) + @member ||= Member.where( + organization_id: current_organization.id, + member_uid: params[:member_uid] + ).first + + # TODO: better not found management please + raise unless @member end def toggle_active_posts current_organization.posts.where(user_id: @member.user_id). each { |post| post.update_attributes(active: false) } end + + # TODO: move to service and probably different controller + def find_transfer_offer + current_organization.offers. + find(params[:offer]) if params[:offer].present? + end + + # TODO: move to service and probably different controller + def find_transfer_source + current_user.members. + find_by(organization: current_organization).account.id + end + + # TODO: move to service and probably different controller + def find_transfer_sources_for_admin + return unless admin? + [current_organization.account] + + current_organization.member_accounts.where("members.active is true") + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 08c47942c..fcd7c31b0 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -60,7 +60,11 @@ def show else model.all.active.of_active_members end - post = scope.find params[:id] + post = scope.find(params[:id]) + @member = Member.where( + organization: post.organization, + user: post.user + ) instance_variable_set("@#{resource}", post) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 68f6fb828..723a57006 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,20 +5,13 @@ def scoped_users current_organization.users end - def index - @users = scoped_users - @memberships = current_organization.members. - where(user_id: @users.map(&:id)). - includes(:account).each_with_object({}) do |mem, ob| - ob[mem.user_id] = mem - end - end - + # GET /users/:id + # def show - @user = find_user - @member = @user.as_member_of(current_organization) - @movements = @member.movements.order("created_at DESC").page(params[:page]). - per(10) + @user = User.find_by_id(params[:id]) + # TODO: better not found management please + raise unless @user + authorize @user end def new @@ -61,18 +54,6 @@ def update end end - def give_time - @user = scoped_users.find(params[:id]) - @destination = @user.members. - find_by(organization: current_organization).account.id - @source = find_transfer_source - @offer = find_transfer_offer - @transfer = Transfer.new(source: @source, - destination: @destination, - post: @offer) - @sources = find_transfer_sources_for_admin - end - private def user_params @@ -85,22 +66,6 @@ def user_params params.require(:user).permit *fields_to_permit end - def find_transfer_offer - current_organization.offers. - find(params[:offer]) if params[:offer].present? - end - - def find_transfer_source - current_user.members. - find_by(organization: current_organization).account.id - end - - def find_transfer_sources_for_admin - return unless admin? - [current_organization.account] + - current_organization.member_accounts.where("members.active is true") - end - def find_user if current_user.id == params[:id].to_i current_user diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 947b95fa0..757cedfc8 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,28 +1,26 @@ module UsersHelper - # TODO refactor or eliminate - poosibly the second. + # TODO refactor or eliminate - possibly the second. def users_as_json - @users = (admin? || superadmin?) ? @users : @users.actives - @users.map do |user| - membership = @memberships[user.id] + @memberships.map do |membership| { - id: user.id, - avatar: avatar_url(user), - member_id: membership.member_uid, - username: user.username, - email: user.email_if_real, - unconfirmed_email: user.unconfirmed_email, - phone: user.phone, - alt_phone: user.alt_phone, + id: membership.user_id, + avatar: avatar_url(membership.user), + member_uid: membership.member_uid, + username: membership.user.username, + email: membership.user.email_if_real, + unconfirmed_email: membership.user.unconfirmed_email, + phone: membership.user.phone, + alt_phone: membership.user.alt_phone, balance: membership.account_balance.to_i, - url: user_path(user), - edit_link: edit_user_path(user), + url: member_path(membership.member_uid), + edit_link: edit_user_path(membership.user), cancel_link: cancel_member_path(membership), toggle_manager_link: toggle_manager_member_path(membership), manager: !!membership.manager, toggle_active_link: toggle_active_member_path(membership), active: membership.active?, - valid_email: user.has_valid_email? + valid_email: membership.user.has_valid_email? } end.to_json.html_safe end diff --git a/app/models/post.rb b/app/models/post.rb index e8b2ee624..25d749d45 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -36,13 +36,16 @@ def self.inherited(child) end end + # TODO: what is this? Can we delete it? attr_reader :member_id belongs_to :category belongs_to :user belongs_to :organization + # TODO: what is this? belongs_to :publisher, class_name: "User", foreign_key: "publisher_id" + # TODO: what is this? Can we delete it? # belongs_to :member, class_name: "Member", foreign_key: "user_id" has_many :user_members, class_name: "Member", through: :user, source: :members has_many :transfers diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index a0b6b3739..504ce20be 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -3,6 +3,14 @@ def new? user.admins?(organization) end + def show? + return true if user.id == record.id + + record.organizations.any? do |org| + user.admins?(org) + end + end + def create? user.admins?(organization) end diff --git a/app/views/account/edit.html.erb b/app/views/account/edit.html.erb new file mode 100644 index 000000000..8b9133d95 --- /dev/null +++ b/app/views/account/edit.html.erb @@ -0,0 +1,6 @@ +
+ ADMIN +
+ <% end %> +
+
+ <%= t(".created_at") %>
+
+ <%= @member.entry_date ? l(@member.entry_date, format: :long) : mdash %>
+
+
+ <%= t(".user_no") %>
+
+ <%= @member.member_uid || mdash %>
+
+
+ <%= t(".balance") %>
+ <%= seconds_to_hm(@member.account.try(:balance) || mdash) %>
+
+
- ADMIN -
- <% end %> -
-
- <%= t(".created_at") %>
-
- <%= @member.entry_date ? l(@member.entry_date, format: :long) : mdash %>
-
-
- <%= t(".user_no") %>
-
- <%= @member.member_uid || mdash %>
-
-
- <%= t(".balance") %>
- <%= seconds_to_hm(@member.account.try(:balance) || mdash) %>
-
-