his_emr_user_management 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 352525399bd80965fc1bef10187afb7e950cb8d53a37df41e9449a5871417e89
4
+ data.tar.gz: 7cbad475132102e2ea911b0423fd663a2ad471d8fb051ca74d751df77d327b19
5
+ SHA512:
6
+ metadata.gz: 49c60961414b77897b9793a16ba6d47b9ab16d4ba9bb46cf1160b85fc7b1b05eafaf32e51f9abe2a47c8b53bb957801eaedcc16023462156353b6c3eb855be0d
7
+ data.tar.gz: d5bb8e23e4575fccfcd0876b1e60e91919026c2dfa005cdb27833a0a8bc9bdc100647ebcc5163dfc7fde7e6a4ffc9531d9a8e150068f8ce977af3974cd6dfd45
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Fredrick Chiyenda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # UserManagement
2
+ Short description and motivation.
3
+ This gem provides generic functionality for EMRs user management to integrate with an LDAP server via the Single sign on Service (sss) just by plugging it into your application(s).
4
+
5
+ ## Usage
6
+ How to use my plugin.
7
+
8
+ The plugin needs to be mounted into your application and the api's it exposes will be available via the mounted point.
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'user_management', :git => 'https://github.com/EGPAFMalawiHIS/emr_usermanagement_engine.git', :branch => 'main'
15
+ ```
16
+
17
+ Add this line to your routes.rb file:
18
+ ```ruby
19
+ mount UserManagement::Engine, at: "/api/v1"
20
+ ```
21
+
22
+ Create a file config/settings.yml and add the following structure:
23
+
24
+ ```yaml
25
+ secret_key: ""
26
+ duplicate_precision: 90
27
+ gateway_key: 'usermanagement'
28
+ sss_url: localhost:3001
29
+ ```
30
+ -- Generate a key and put it on the secret_key value and update the other key as necessary use command 'rake secret'
31
+
32
+ Add the following code to your application_controller.rb
33
+ ```ruby
34
+ before_action :authorized_user
35
+
36
+ def authorized_user
37
+ token = request.headers[:Authorization]
38
+ response = JSON.parse(UserManagement::ApplicationController.authorized(token))
39
+ return render json: {status: 403, message: 'User not authorised or token expired'} if response['status'] == 403
40
+ return render json: {status: 401, message: 'Invalid username or password'} if response['status'] == 401
41
+ end
42
+ ```
43
+
44
+ And then execute:
45
+ ```bash
46
+ $ bundle i
47
+ ```
48
+ In Rails.root of your application execute:
49
+ ```ruby
50
+ bin/rails user_management:install:migrations
51
+ ```
52
+ ```ruby
53
+ bin/rails db:migrate
54
+ ```
55
+ ## Contributing
56
+ Contribution directions go here.
57
+
58
+ ## License
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'UserManagement'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,39 @@
1
+ module UserManagement
2
+ SETTINGS = YAML.load_file("#{Rails.root}/config/settings.yml")
3
+ class ApplicationController < ActionController::API
4
+
5
+ # protect_from_forgery with: :exception
6
+
7
+ def encode_token(payload)
8
+ payload[:exp] = 8.hours.from_now.to_i
9
+ JWT.encode(payload, SETTINGS['secret_key'])
10
+ end
11
+
12
+ def self.decode_token(token)
13
+ begin
14
+ token = token.split(' ')[1]
15
+ JWT.decode(token, SETTINGS['secret_key'], true, algorithm: 'HS256')
16
+ rescue JWT::DecodeError
17
+ nil
18
+ end
19
+ end
20
+
21
+ def self.logged_in_user(token)
22
+ decoded_token = decode_token(token)[0].symbolize_keys rescue return
23
+ if decoded_token
24
+ user_id = decoded_token[:user_id]
25
+ @user = User.find(user_id) if decoded_token[:exp] > Time.now.to_i
26
+ end
27
+ end
28
+
29
+ def self.logged_in?(token)
30
+ !!logged_in_user(token)
31
+ end
32
+
33
+ def self.authorized(token)
34
+ return render json: {message: 'Token not provided',status: 403}.symbolize_keys unless token
35
+ return render json: { message: 'Please log in', status: 403 }.symbolize_keys unless logged_in?(token)
36
+ render json: {status: 200, message: 'Authorised User'}
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,67 @@
1
+ require_dependency "user_management/application_controller"
2
+
3
+ module UserManagement
4
+ class AuthenticationController < ApplicationController
5
+ require 'rest-client'
6
+ before_action :authorized, only: [:auto_login]
7
+
8
+ def create
9
+ #Contact sss
10
+ @user = User.find_by(username: params[:username])
11
+ begin
12
+ response = JSON.parse(RestClient.post("#{SETTINGS['sss_url']}/authenticate",{'username' => params[:username],'password' => params[:password]}.to_json, {content_type: :json, accept: :json}))
13
+ rescue => e
14
+ return authenticate_locally
15
+ end
16
+ response.symbolize_keys!
17
+ if response[:isValid] == true
18
+ if @user.blank?
19
+ ActiveRecord::Base.transaction do
20
+ #Remove hard coding
21
+ person = Person.create!(first_name: (response[:first_name] || 'N/A'),surname: (response[:last_name] || 'N/A'), sex: (response[:gender] || 'N'))
22
+ user = User.create!(username: params[:username], email: 'test@example.com', person_id:person.id, password:params[:password],password_confirmation: params[:password_confirmation])
23
+ @user = User.find_by(username: params[:username])
24
+ end
25
+ else
26
+ ActiveRecord::Base.transaction do
27
+ #Remove hard coding
28
+ Person.find_by(person_id: @user.person_id).update(first_name: (response[:first_name] || 'N/A'),surname: (response[:last_name] || 'N'), sex: (response[:gender] || 'N'))
29
+ @user.update!(email: 'test@example.com', password:params[:password],password_confirmation: params[:password_confirmation])
30
+ end
31
+ end
32
+ authenticate_locally
33
+ elsif response[:isValid] == false
34
+ if @user
35
+ ActiveRecord::Base.connection.execute("UPDATE user set password='password',password_digest='digest' WHERE user_id = #{@user.id}") unless @user
36
+ render json: {error: "Invalid username or password", status: 403}, status: :forbidden
37
+ else
38
+ authenticate_locally
39
+ end
40
+ elsif response['error']
41
+ authenticate_locally
42
+ end
43
+ end
44
+
45
+ def authenticate_locally
46
+ if @user && (@user.authenticate(params[:password]) rescue false)
47
+ token = encode_token({user_id: @user.id, kid: SETTINGS['gateway_key']})
48
+ person = Person.find(@user.person_id)
49
+ render json: {
50
+ "access_token":token,
51
+ "token_type":"bearer",
52
+ "expires_in":28800,
53
+ "username": @user.username,
54
+ "firstname": person.first_name,
55
+ "surname": person.surname,
56
+ "gender": person.sex
57
+ }
58
+ else
59
+ render json: {error: "Invalid username or password",status: 401}, status: :unauthorized
60
+ end
61
+ end
62
+
63
+ def auto_login
64
+ render json: @user
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,92 @@
1
+ require_dependency "user_management/application_controller"
2
+
3
+ module UserManagement
4
+ class UsersController < ApplicationController
5
+ def index
6
+ render json: User.all ,status: 200
7
+ end
8
+ def create
9
+ person = Person.create(first_name: params[:first_name],surname: params[:surname], sex: params[:sex])
10
+ user = User.new(username: params[:username], email: params[:email], person_id:person.id, password:params[:password],password_confirmation: params[:password_confirmation])
11
+ if user.save
12
+ render json: user, status: :created
13
+ else
14
+ render json: user.error, status: :unprocessable_entity
15
+ end
16
+ end
17
+
18
+ def show
19
+ user = User.find(params[:id]) rescue nil
20
+ if user.nil?
21
+ render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
22
+ else
23
+ render json: user, status: 200
24
+ end
25
+ end
26
+
27
+ def update
28
+ user = User.find(params[:id]) rescue nil
29
+ person = Person.find(user.person_id)
30
+ if user.nil?
31
+ render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
32
+ else
33
+ params.keys.each do |key|
34
+ next if ["username","email","password"].exclude?(key)
35
+ user[key] = params[key]
36
+ end
37
+
38
+ params.keys.each do |key|
39
+ next if ["first_name","surname","sex"].exclude?(key)
40
+ person[key] = params[key]
41
+ end
42
+
43
+ if user.save && person.save
44
+ render json: user, status: 200
45
+ else
46
+ render json: user.error, status: :unprocessable_entity
47
+ end
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ user = User.find(params[:id])
53
+ if user.destroy!
54
+ render json: {message: 'Record Deleted'}, status: :no_content
55
+
56
+ else
57
+ render json: user.error, status: :unprocessable_entity
58
+ end
59
+ end
60
+
61
+ def add_privilege
62
+ user = User.find(params[:id]) rescue nil
63
+ if user.nil?
64
+ render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
65
+ else
66
+ privilege_id = Privilege.where(name: params[:name]).first.id
67
+ user_privilege = UserPrivilege.new(user_id: user.id, privilege_id: privilege_id, active: 1 )
68
+ if user_privilege.save
69
+ render json: user_privilege, status: :created
70
+ else
71
+ render json: user_privilege.error, status: :unprocessable_entity
72
+ end
73
+ end
74
+ end
75
+
76
+ def delete_privilege
77
+ user = User.find(params[:id]) rescue nil
78
+ privilege_id = Privilege.where(name: params[:name]).first.id
79
+ user_privilege = UserPrivilege.where(user_id: user.id, privilege_id: privilege_id, active: 1 ).first rescue nil
80
+ if user.nil? || user_privilege.nil?
81
+ render json: {message: 'No user or privilege with id #{params[:id]}'}, status: :no_content
82
+ else
83
+ user_privilege.active = 1
84
+ if user_privilege.save
85
+ render json: user_privilege, status: :accepted
86
+ else
87
+ render json: user_privilege.error, status: :unprocessable_entity
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,4 @@
1
+ module UserManagement
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module UserManagement
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module UserManagement
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module UserManagement
2
+ class Person < ApplicationRecord
3
+ validates :first_name , presence: true
4
+ validates :surname , presence: true
5
+ validates :sex , presence: true
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module UserManagement
2
+ class Privilege < ApplicationRecord
3
+ validates :name, presence: true
4
+ validates :description, presence: true
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module UserManagement
2
+ class User < ApplicationRecord
3
+ has_secure_password
4
+ validates :username, presence: true,uniqueness: true
5
+ validates :email, presence: true
6
+ validates :person_id, presence: true
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module UserManagement
2
+ class UserPrivilege < ApplicationRecord
3
+ validates :user_id, presence: true
4
+ validates :privilege_id, presence: true
5
+ end
6
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,10 @@
1
+ UserManagement::Engine.routes.draw do
2
+ resources :users do
3
+ member do
4
+ put '/add_privilege' => 'users#add_privilege'
5
+ delete '/delete_privilege' => 'users#delete_privilege'
6
+ end
7
+ end
8
+ post '/token', to: 'authentication#create'
9
+ resources :authentication
10
+ end
@@ -0,0 +1,4 @@
1
+ secret_key: "745840cc554aed7519acb760ae3b05f850c74d076ca21ff15891f9dfaddad69426b23090637e17645baee710964245780ebb6e59606b97bb8764959acb40a853"
2
+ duplicate_precision: 90
3
+ gateway_key: 'usermanagement'
4
+ sss_url: localhost:3001
@@ -0,0 +1,16 @@
1
+ class CreateUserManagementPeople < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :user_management_people, id: false, if_not_exists: true do |t|
4
+ t.integer :person_id, null: false, primary_key: true
5
+ t.string :national_id
6
+ t.string :first_name, null: false
7
+ t.string :surname, null: false
8
+ t.string :other_name
9
+ t.date :birthdate
10
+ t.string :sex, limit: 1
11
+ t.integer :phone_number
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ class CreateUserManagementPrivileges < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :user_management_privileges do |t|
4
+ t.string :name, null: false
5
+ t.text :description, null: false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ class CreateUserManagementUsers < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :user_management_users, id: false do |t|
4
+ t.integer :user_id, null: false, primary_key: true
5
+ t.string :username, null: false
6
+ t.string :email
7
+ t.string :password
8
+ t.string :password_digest
9
+ t.integer :person_id, null: false
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUserManagementUserPrivileges < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :user_management_user_privileges, id: false do |t|
4
+ t.integer :user_privilege_id, null: false, primary_key: true
5
+ t.integer :user_id, null: false
6
+ t.integer :privilege_id, null: false
7
+ t.boolean :active, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class AddForeignKeyContraints < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_foreign_key :user_management_user_privileges, :user_management_users, column: :user_id, primary_key: :user_id
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :user_management do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,11 @@
1
+ module UserManagement
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace UserManagement
4
+ config.generators.api_only = true
5
+
6
+ require 'rubygems'
7
+ require 'rest-client'
8
+ require 'bcrypt'
9
+ require 'jwt'
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module UserManagement
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require "user_management/engine"
2
+
3
+ module UserManagement
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: his_emr_user_management
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fredrick Chiyenda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bcrypt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Description of UserManagement.
84
+ email:
85
+ - fchiyenda@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - app/controllers/user_management/application_controller.rb
94
+ - app/controllers/user_management/authentication_controller.rb
95
+ - app/controllers/user_management/users_controller.rb
96
+ - app/jobs/user_management/application_job.rb
97
+ - app/mailers/user_management/application_mailer.rb
98
+ - app/models/user_management/application_record.rb
99
+ - app/models/user_management/person.rb
100
+ - app/models/user_management/privilege.rb
101
+ - app/models/user_management/user.rb
102
+ - app/models/user_management/user_privilege.rb
103
+ - config/routes.rb
104
+ - config/settings.yml.example
105
+ - db/migrate/20210613151258_create_user_management_people.rb
106
+ - db/migrate/20210613152557_create_user_management_privileges.rb
107
+ - db/migrate/20210613152630_create_user_management_users.rb
108
+ - db/migrate/20210613152744_create_user_management_user_privileges.rb
109
+ - db/migrate/20210614023528_add_foreign_key_contraints.rb
110
+ - lib/tasks/user_management_tasks.rake
111
+ - lib/user_management.rb
112
+ - lib/user_management/engine.rb
113
+ - lib/user_management/version.rb
114
+ homepage: https://github.com/EGPAFMalawiHIS/emr_usermanagement_engine
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ source_code_uri: https://github.com/EGPAFMalawiHIS/emr_usermanagement_engine
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubygems_version: 3.4.5
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Summary of UserManagement.
138
+ test_files: []