diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb new file mode 100644 index 0000000..4a11646 --- /dev/null +++ b/app/controllers/accounts_controller.rb @@ -0,0 +1,78 @@ +class AccountsController < ApplicationController + before_action :set_user, only: [:show, :edit, :update, :destroy] + before_action :admin? + + def index + @users = User.all + end + + def show + end + + def new + @user = User.new + end + + def edit + if @user.is_admin? + redirect_to users_path, notice: 'Admins are not editable by other admins.' + end + end + + def create + @user = User.new(user_params) + + respond_to do |format| + if @user.save + format.html { redirect_to @user, notice: 'User was successfully created.' } + format.json { render action: 'show', status: :created, location: @user } + else + format.html { render action: 'new' } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + def update + password = params[:user][:current_password] + params[:user].delete(:current_password) + respond_to do |format| + if @user.update(user_params) + format.html { redirect_to @user, notice: 'User was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: 'edit', notice: @user.errors } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /users/1 + # DELETE /users/1.json + def destroy + @user.destroy + respond_to do |format| + format.html { redirect_to users_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_user + @user = User.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def user_params + params.require(:user).permit(:email, :password, :password_confirmation, :current_password) + end + + + def admin? + if current_admin.nil? + redirect_to new_admin_session_path, notice: 'Please login as an Admin.' + end + + end +end diff --git a/app/models/user.rb b/app/models/user.rb index e9743c2..5168d09 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,5 +21,8 @@ class User < ActiveRecord::Base end end + def is_admin? + Admin.exists?(email: self.email) + end end diff --git a/app/models/.keep b/app/views/accounts/_form.html.erb similarity index 100% rename from app/models/.keep rename to app/views/accounts/_form.html.erb diff --git a/app/views/accounts/edit.html.erb b/app/views/accounts/edit.html.erb new file mode 100644 index 0000000..fb34d84 --- /dev/null +++ b/app/views/accounts/edit.html.erb @@ -0,0 +1,19 @@ +

Edit <%= @user.email %>

+ +<%= simple_form_for(@user, url: user_path(@user), html: { method: :put }) do |f| %> + <%= f.error_notification %> + +
+ <%= f.input :email, required: true, autofocus: true %> + + <%= f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false %> + <%= f.input :password_confirmation, required: false %> +
+ +
+ <%= f.button :submit, "Update" %> +
+<% end %> + + +<%= link_to "Back", :back %> \ No newline at end of file diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb new file mode 100644 index 0000000..e9f7566 --- /dev/null +++ b/app/views/accounts/index.html.erb @@ -0,0 +1,24 @@ +

Users

+
+

To edit your own user, use the Profile Option.

+ + + + + + + + + + <% @users.each do |user| %> + <% next if user.is_admin? %> + <% next if user.equal?(current_user) %> + + + + + + <% end %> + +
Email
<%= link_to user.email, user %><%= link_to 'Edit', edit_user_path(user) %><%= link_to 'Destroy', user_path(user), method: :delete, data: { confirm: 'Are you sure?' } %>
+<%= link_to 'New user', new_user_path unless Rails.env.production? %> diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb new file mode 100644 index 0000000..bec0242 --- /dev/null +++ b/app/views/accounts/new.html.erb @@ -0,0 +1,15 @@ +

New User

+<%= simple_form_for(@user, url: users_path) do |f| %> + <%= f.error_notification %> + +
+ <%= f.input :email, required: true, autofocus: true %> + + <%= f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false %> + <%= f.input :password_confirmation, required: false %> +
+ +
+ <%= f.button :submit, "Update" %> +
+<% end %> \ No newline at end of file diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb new file mode 100644 index 0000000..1935596 --- /dev/null +++ b/app/views/accounts/show.html.erb @@ -0,0 +1 @@ +

<%= @user.email %>

diff --git a/app/views/campaigns/index.html.erb b/app/views/campaigns/index.html.erb index 9d0db2a..5caf4b9 100644 --- a/app/views/campaigns/index.html.erb +++ b/app/views/campaigns/index.html.erb @@ -27,7 +27,7 @@ <%= campaign.state %> <%= campaign.zip %> <%= link_to 'Edit', edit_campaign_path(campaign.listingcode) unless campaign.sent %> - <%= link_to 'Destroy', campaign_path(campaign.listingcode), method: :delete, data: { confirm: 'Are you sure?' } %> + <%= link_to 'Destroy', campaign_path(campaign.listingcode), method: :delete, data: { confirm: 'Are you sure?' } if admin_signed_in? %> <% end %> diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4ae4f2e..901b232 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -11,6 +11,13 @@