Sfoglia il codice sorgente

Ajout d'un controller admin::users

Lou 3 anni fa
parent
commit
1fdd0a2f9c

+ 2
- 0
app/controllers/api/v1/admin/users_controller.rb Vedi File

@@ -0,0 +1,2 @@
1
+class Api::V1::Admin::UsersController < ApplicationController
2
+end

+ 4
- 0
app/controllers/concerns/authenticable.rb Vedi File

@@ -15,4 +15,8 @@ module Authenticable
15 15
   def check_login
16 16
     head :forbidden unless self.current_user
17 17
   end
18
+
19
+  def is_admin?
20
+    head :forbidden unless self.current_user.admin
21
+  end
18 22
 end

+ 1
- 0
config/database.yml Vedi File

@@ -9,6 +9,7 @@ default: &default
9 9
   adapter: postgresql
10 10
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
11 11
   timeout: 5000
12
+  username: lou
12 13
 
13 14
 development:
14 15
   <<: *default

+ 7
- 0
config/routes.rb Vedi File

@@ -8,5 +8,12 @@ Rails.application.routes.draw do
8 8
       end
9 9
       resources :tasks
10 10
     end
11
+    namespace :admin do
12
+      resources :users
13
+      resources :token, only: %i[create]
14
+      resources :activities do
15
+        resources :tasks
16
+      end
17
+    end
11 18
   end
12 19
 end

+ 1
- 0
db/migrate/20210413093535_create_users.rb Vedi File

@@ -6,6 +6,7 @@ class CreateUsers < ActiveRecord::Migration[6.1]
6 6
       t.string :username, null: false
7 7
       t.index :username, unique: true
8 8
       t.string :password_digest
9
+      t.boolean :admin, default: false   
9 10
 
10 11
       t.timestamps
11 12
     end

+ 1
- 0
db/schema.rb Vedi File

@@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 2021_05_10_133939) do
52 52
     t.string "email", null: false
53 53
     t.string "username", null: false
54 54
     t.string "password_digest"
55
+    t.boolean "admin", default: false
55 56
     t.datetime "created_at", precision: 6, null: false
56 57
     t.datetime "updated_at", precision: 6, null: false
57 58
     t.index ["email"], name: "index_users_on_email", unique: true

+ 5
- 1
db/seeds.rb Vedi File

@@ -29,6 +29,9 @@ Task.reset_pk_sequence
29 29
   end
30 30
 end
31 31
 
32
+User.create! username: "admin", email: "admin@email.com", password: "azerty", admin: true
33
+puts "Created admin"
34
+
32 35
 30.times do |i|
33 36
   task = Task.create(
34 37
     name: Faker::Fantasy::Tolkien.character,
@@ -38,4 +41,5 @@ end
38 41
     activity_id: Activity.all.sample.id
39 42
   )
40 43
   puts "Created TASK ##{i} - #{task.name}"
41
-end
44
+end
45
+

+ 7
- 0
test/controllers/api/v1/admin/users_controller_test.rb Vedi File

@@ -0,0 +1,7 @@
1
+require "test_helper"
2
+
3
+class Api::V1::Admin::UsersControllerTest < ActionDispatch::IntegrationTest
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end

Loading…
Annulla
Salva