cats_core 1.0.8 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45dbf26292f9ebc98fb50e4195e44773184d3f460c0d5aadb3c45afcffffa752
4
- data.tar.gz: 7cab2b31272ec765c42d8cd8724f1d8f3daff1481fd82057edf08ed40ce389b3
3
+ metadata.gz: ad0f19c4e9e9d38a4038b2f94ca78ea18fb730dccb1c04bacf403a8fe5b29de5
4
+ data.tar.gz: 452ec1bb9126955274e5adae51d60a903e88674da97610b1b9945c43995d1ab6
5
5
  SHA512:
6
- metadata.gz: 6017835a41ce450bb778aea397234083ac257ec74b1f995a5087811dfae0db693172468ceed14c11ef8433dd26eca298436256ff8fc876a7482e8ce2610d46bb
7
- data.tar.gz: 87ff5af9c3533a23b57ede84a3547818b7f54e51cfb629dbd8f3c26c6de73900f974c2ecb0bc38c999b01bdd29a0b7260daf4ec03510407fae8511d85ae372d3
6
+ metadata.gz: 40dcc3d2d091d837d24da04a8889bca46b8afa22662c27fa518fb3ef9b98c2b20aad829f3f5126e444566e6229ca67d954dfa0ac7f76e93c4f03ef80c40fded4
7
+ data.tar.gz: ad35c7773f8b1ea0bf10193d70e0857096da4768029c4b1de675b1a9c005e0d6fba86f8201739fafde97122a137a2122bd4aa12b506b965a053a2606f963d418
@@ -0,0 +1,37 @@
1
+ module Cats
2
+ module Core
3
+ class AccessController < ApplicationController
4
+ skip_before_action :authenticate, only: [:login]
5
+
6
+ def login
7
+ user = Cats::Core::User.find_by(email: auth_params[:email])
8
+ if user
9
+ if user.authenticate(auth_params[:password])
10
+ roles = user.roles.map(&:name)
11
+
12
+ unless roles.count.positive?
13
+ render json: { error: 'User has no roles.' }, status: :unprocessable_entity
14
+ return
15
+ end
16
+
17
+ payload = {
18
+ id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles
19
+ }
20
+ jwt = Cats::Core::TokenAuthService.issue(payload)
21
+ render json: { token: jwt, user: payload }
22
+ else
23
+ rener json: { error: 'Invalid username or password.' }, status: 400
24
+ end
25
+ else
26
+ render json: { error: 'User does not exist.' }, status: 400
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def auth_params
33
+ params.require(:auth).permit(:email, :password)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,6 +1,6 @@
1
1
  module Cats
2
2
  module Core
3
- class ApplicationController < ActionController::Base
3
+ class ApplicationController < ActionController::API
4
4
  before_action :authenticate
5
5
 
6
6
  def current_user
@@ -0,0 +1,19 @@
1
+ module Cats
2
+ module Core
3
+ class MenusController < ApplicationController
4
+ before_action :set_service
5
+
6
+ def index
7
+ menu = @service.fetch_menu(current_user)
8
+ data = ActiveModelSerializers::SerializableResource.new(menu, each_serializer: RoleMenuSerializer)
9
+ render json: { success: true, data: data }
10
+ end
11
+
12
+ private
13
+
14
+ def set_service
15
+ @service = MenuService.new
16
+ end
17
+ end
18
+ end
19
+ end
@@ -11,7 +11,7 @@ module Cats
11
11
  private
12
12
 
13
13
  def set_role
14
- @role = Role.find(params[:id])
14
+ @role = Role.find_by(name: params[:name])
15
15
  end
16
16
  end
17
17
  end
@@ -6,7 +6,7 @@ module Cats
6
6
  belongs_to :recipient, polymorphic: true
7
7
 
8
8
  def message
9
- { id: id, read: !read_at.nil? }.merge(to_notification.message)
9
+ { id: id, read: !read_at.nil?, created_at: created_at }.merge(to_notification.message)
10
10
  end
11
11
 
12
12
  def self.messages(notifications)
@@ -14,6 +14,9 @@ module Cats
14
14
 
15
15
  errors.add(:base, 'Application module does not match for role and menu')
16
16
  end
17
+
18
+ delegate(:label, to: :menu, prefix: false)
19
+ delegate(:icon, to: :menu, prefix: false)
17
20
  end
18
21
  end
19
22
  end
@@ -1,9 +1,20 @@
1
1
  # This is a simple notification class which delivers a
2
2
  # message via database delivery method. It requires a
3
- # +msg+ parameter to instantiate, which represents the
3
+ # +body+ and +title+ parameter to instantiate, which represents the
4
4
  # text message to deliver.
5
- class SimpleNotification < Noticed::Base
6
- deliver_by :database
5
+ module Cats
6
+ module Core
7
+ class SimpleNotification < Noticed::Base
8
+ deliver_by :database
7
9
 
8
- param :msg
10
+ param :body
11
+ param :title
12
+
13
+ def message
14
+ title = params[:title]
15
+ body = params[:body]
16
+ { title: title, body: body }
17
+ end
18
+ end
19
+ end
9
20
  end
@@ -0,0 +1,11 @@
1
+ module Cats
2
+ module Core
3
+ class RoleMenuSerializer < ActiveModel::Serializer
4
+ attributes :id, :label, :icon, :description, :menu_items
5
+
6
+ def menu_items
7
+ object.menu.menu_items
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Cats
2
+ module Core
3
+ class MenuService
4
+ def fetch_menu(user)
5
+ user.roles.map(&:role_menus).flatten
6
+ end
7
+ end
8
+ end
9
+ end
data/config/routes.rb CHANGED
@@ -1,14 +1,16 @@
1
1
  Cats::Core::Engine.routes.draw do
2
+ post '/login', controller: :access, action: :login
2
3
  get '/notifications/unread', controller: :notifications, action: :unread
3
4
  get '/notifications/read', controller: :notifications, action: :read
4
-
5
+
6
+ resources :menus, only: [:index]
5
7
  resources :notifications, only: [:index] do
6
8
  member do
7
9
  post 'mark_as_read', controller: :notifications, action: :mark_as_read
8
10
  post 'mark_as_unread', controller: :notifications, action: :mark_as_unread
9
11
  end
10
12
  end
11
- resources :roles do
13
+ resources :roles, param: :name do
12
14
  member do
13
15
  get 'users', controller: :roles, action: :users
14
16
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.0.8'.freeze
3
+ VERSION = '1.0.12'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-05 00:00:00.000000000 Z
11
+ date: 2021-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -216,7 +216,9 @@ files:
216
216
  - MIT-LICENSE
217
217
  - README.md
218
218
  - Rakefile
219
+ - app/controllers/cats/core/access_controller.rb
219
220
  - app/controllers/cats/core/application_controller.rb
221
+ - app/controllers/cats/core/menus_controller.rb
220
222
  - app/controllers/cats/core/notifications_controller.rb
221
223
  - app/controllers/cats/core/roles_controller.rb
222
224
  - app/controllers/cats/core/users_controller.rb
@@ -244,6 +246,8 @@ files:
244
246
  - app/models/cats/core/way_bill.rb
245
247
  - app/models/cats/core/way_bill_item.rb
246
248
  - app/notifications/cats/core/simple_notification.rb
249
+ - app/serializers/cats/core/role_menu_serializer.rb
250
+ - app/services/cats/core/menu_service.rb
247
251
  - app/services/cats/core/notification_service.rb
248
252
  - app/services/cats/core/token_auth_service.rb
249
253
  - config/routes.rb