Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source "http://rubygems.org"
# frozen_string_literal: true

source 'http://rubygems.org'

# Declare your gem's dependencies in samfundet_auth.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
Expand Down
9 changes: 4 additions & 5 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env rake
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -20,11 +22,9 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
load 'rails/tasks/engine.rake'



Bundler::GemHelper.install_tasks

require 'rake/testtask'
Expand All @@ -36,5 +36,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end


task :default => :test
task default: :test
28 changes: 13 additions & 15 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

class Member < ActiveRecord::Base
self.primary_key = :medlem_id

has_many :members_roles, :dependent => :destroy
has_many :roles, :through => :members_roles
has_many :members_roles, dependent: :destroy
has_many :roles, through: :members_roles

attr_accessor :passord if %w(production staging).include? Rails.env
attr_accessor :passord if %w[production staging].include? Rails.env

validates_presence_of :fornavn, :etternavn, :mail, :telefon

if Rails.env.development?
validates_presence_of :passord
end
validates_presence_of :passord if Rails.env.development?

def firstname
fornavn
Expand All @@ -25,7 +25,7 @@ def full_name
end

def self.authenticate(member_id_or_email, password)
if %w(production staging).include? Rails.env
if %w[production staging].include? Rails.env
authenticate_production member_id_or_email, password
else
authenticate_development member_id_or_email, password
Expand All @@ -37,22 +37,20 @@ def self.authenticate(member_id_or_email, password)
def self.authenticate_production(member_id_or_email, password)
Rails.logger.silence do # Prevents passwords from showing up in the logs.
member_id = connection.select_value sanitize_sql([
"SELECT * FROM sett_lim_utvidet_medlemsinfo(?, ?)",
member_id_or_email.to_s,
password
'SELECT * FROM sett_lim_utvidet_medlemsinfo(?, ?)',
member_id_or_email.to_s,
password
])
unless member_id.nil?
Member.find member_id
end
Member.find member_id unless member_id.nil?
end
end

def self.authenticate_development(member_id_or_email, password)
# There are no SQL standard for lower case search,
# and that's why there's no help from ruby or rails.
member = Member.find_by_medlem_id(member_id_or_email) ||
Member.where("lower(mail) = ?", member_id_or_email.downcase).first
Member.where('lower(mail) = ?', member_id_or_email.downcase).first

member if member and member.passord == password
member if member && (member.passord == password)
end
end
2 changes: 2 additions & 0 deletions app/models/members_role.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class MembersRole < ActiveRecord::Base
validates_presence_of :member_id
validates_presence_of :role_id
Expand Down
8 changes: 5 additions & 3 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

class Role < ActiveRecord::Base
validates_format_of :title, :with => /\A[a-z0-9\_\-]+\z/
validates_format_of :title, with: /\A[a-z0-9\_\-]+\z/
validates_presence_of :name, :description

attr_readonly :title

default_scope { order(:title) }

has_many :members_roles, :dependent => :destroy
has_many :members, :through => :members_roles
has_many :members_roles, dependent: :destroy
has_many :members, through: :members_roles
has_many :roles
belongs_to :group
belongs_to :role
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

SamfundetAuth::Engine.routes.draw do
end
4 changes: 3 additions & 1 deletion db/migrate/20121031220629_create_members.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class CreateMembers < ActiveRecord::Migration
def up
create_table :members, :primary_key => :medlem_id do |t|
create_table :members, primary_key: :medlem_id do |t|
t.string :fornavn
t.string :etternavn
t.string :mail
Expand Down
4 changes: 3 additions & 1 deletion db/migrate/20121031220750_create_roles.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

class CreateRoles < ActiveRecord::Migration
def up
create_table :roles do |t|
t.string :name # The name is a generic name of that type of role. E.g. «Gjengsjef» is a name.
t.string :title # The title of a role is unique and the properties gained from a role depends on its title. E.g. «mg_gjengsjef» is a title.
t.text :description
t.boolean :show_in_hierarchy, :default => false
t.boolean :show_in_hierarchy, default: false
t.references :role
t.references :group

Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20121031220820_create_members_roles.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreateMembersRoles < ActiveRecord::Migration
def up
create_table :members_roles do |t|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddForeignKeysForRolesAndMemberRoles < ActiveRecord::Migration
def up
add_foreign_key :roles, :roles
Expand Down
12 changes: 6 additions & 6 deletions lib/samfundet_auth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "active_support/configurable"
require "samfundet_auth/engine"
# frozen_string_literal: true

require 'active_support/configurable'
require 'samfundet_auth/engine'

module SamfundetAuth
include ActiveSupport::Configurable
Expand All @@ -10,7 +12,7 @@ def setup

database_path = "#{Rails.root}/config/database.yml"

if File.exists? database_path
if File.exist? database_path
database_config = YAML.load_file database_path

if config.domain_database
Expand All @@ -23,9 +25,7 @@ def setup
Member.establish_connection database_config[config.member_database.to_s]
end

if config.member_table
Member.table_name = config.member_table.to_s
end
Member.table_name = config.member_table.to_s if config.member_table
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/samfundet_auth/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module SamfundetAuth
class Engine < ::Rails::Engine
isolate_namespace SamfundetAuth
Expand Down
4 changes: 3 additions & 1 deletion lib/samfundet_auth/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module SamfundetAuth
VERSION = "0.2.0"
VERSION = '0.2.0'
end
75 changes: 19 additions & 56 deletions lib/tasks/db_seed.rake
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# encoding: UTF-8
# frozen_string_literal: true

desc "Load the seed data from SamfundetAuth's db/seeds.rb"
namespace :samfundet_auth_engine do
namespace :db do
task :seed => :environment do
task seed: :environment do
if Rake::Task.task_defined? 'samfundet_domain_engine:db:seed'
Rake::Task['samfundet_domain_engine:db:seed'].invoke
else
raise "SamfundetAuth depends upon SamfundetDomain. Have you forgotten to include it in your Gemfile?"
raise 'SamfundetAuth depends upon SamfundetDomain. Have you forgotten to include it in your Gemfile?'
end

print "Deleting existing members and roles, and proceeding with creating new ones.. "
print 'Deleting existing members and roles, and proceeding with creating new ones.. '

tasks = []

[MembersRole, Member, Role].each do |model|
tasks << Proc.new do
tasks << proc do
model.delete_all
end
end

Group.all.each do |group|
tasks << Proc.new do
tasks << proc do
Role.find_or_create_by(title: group.group_leader_role.to_s) do |role|
role.name = "Gjensjef"
role.name = 'Gjensjef'
role.description = "Rolle for gjengsjef for #{group.name}"
role.group = group
end
end

tasks << Proc.new do
tasks << proc do
Role.find_or_create_by(title: group.short_name.parameterize) do |role|
role.name = group.name,
role.description = "Rolle for alle medlemmer av #{group.name}."
role.description = "Rolle for alle medlemmer av #{group.name}."
role.group = group
role.role = Role.find_by_title(group.group_leader_role.to_s)
end
Expand All @@ -42,72 +42,35 @@ namespace :samfundet_auth_engine do
# Just to make sure the variable will exist in this scope.
lim_web_role = Role.new

tasks << Proc.new do
tasks << proc do
lim_web_role = Role.create!(
:title => "lim_web",
:name => "Superuser",
:description => "Superrolle for alle i MG::Web."
title: 'lim_web',
name: 'Superuser',
description: 'Superrolle for alle i MG::Web.'
)
end

members = [
{ :fornavn => "Sondre", :etternavn => "Basma", :mail => "[email protected]" },
{ :fornavn => "Andreas", :etternavn => "Hammar", :mail => "[email protected]" },
{ :fornavn => "Torstein", :etternavn => "Nicolaysen", :mail => "[email protected]" },
{ :fornavn => "Stig", :etternavn => "Hornang", :mail => "[email protected]" },
{ :fornavn => "Erik", :etternavn => "Smistad", :mail => "[email protected]" },
{ :fornavn => "Olav", :etternavn => "Bjørkøy", :mail => "[email protected]" },
{ :fornavn => "Jonas", :etternavn => "Amundsen", :mail => "[email protected]" },
{ :fornavn => "Jonas", :etternavn => "Myrlund", :mail => "[email protected]" },
{ :fornavn => "Stian", :etternavn => "Møllersen", :mail => "[email protected]" },
{ :fornavn => "Håkon", :etternavn => "Sandsmark", :mail => "[email protected]" },
{ :fornavn => "Lorents", :etternavn => "Gravås", :mail => "[email protected]" },
{ :fornavn => "Anders", :etternavn => "Eldhuset", :mail => "[email protected]" },
{ :fornavn => "Aleksander", :etternavn => "Burkow", :mail => "[email protected]" },
{ :fornavn => "Rune", :etternavn => "Holmgren", :mail => "[email protected]" },
{ :fornavn => "Morten", :etternavn => "Lysgaard", :mail => "[email protected]" },
{ :fornavn => "Dagrun", :etternavn => "Haugland", :mail => "[email protected]" },
{ :fornavn => "Christoffer",:etternavn => "Tønnessen", :mail => "[email protected]" },
{ :fornavn => "Trygve", :etternavn => "Bærland", :mail => "[email protected]" },
{ :fornavn => "Odd", :etternavn => "Trondrud", :mail => "[email protected]" },
{ :fornavn => "Asbjørn", :etternavn => "Steinskog", :mail => "[email protected]" },
{ :fornavn => "Simon", :etternavn => "Randby", :mail => "[email protected]" },
{ :fornavn => "Alf", :etternavn => "Jonassen", :mail => "[email protected]" },
{ :fornavn => "Glenn", :etternavn => "Aarøen", :mail => "[email protected]" },
{ :fornavn => "Katrine", :etternavn => "Jordheim", :mail => "[email protected]" },
{ :fornavn => "Simon", :etternavn => "Kvannli", :mail => "[email protected]" },
{ :fornavn => "Filip", :etternavn => "Egge", :mail => "[email protected]" },
{ :fornavn => "Stian", :etternavn => "Steinbakken", :mail => "[email protected]" },
{ :fornavn => "Anders", :etternavn => "Sørby", :mail => "[email protected]" },
{ :fornavn => "Erlend", :etternavn => "Ekern", :mail => "[email protected]" },
{ :fornavn => "Kim Isak", :etternavn => "Olsen", :mail => "[email protected]" },
{ :fornavn => "Konstantin", :etternavn => "Mathisen", :mail => "[email protected]" },
{ :fornavn => "Tollef", :etternavn => "Jørgensen", :mail => "[email protected]" },
{ :fornavn => "Eivind", :etternavn => "Reime", :mail => "[email protected]" },
{ :fornavn => "Petter", :etternavn => "Foss", :mail => "[email protected]" },
{ :fornavn => "Kevin", :etternavn => "Kristiansen", :mail => "[email protected]" },
{ :fornavn => "Eirik", :etternavn => "Vale Aase", :mail => "[email protected]" },
{ :fornavn => "Daria", :etternavn => "Barjaktarevic", :mail => "[email protected]" }

{ fornavn: 'Alf', etternavn: 'Jonassen', mail: '[email protected]' }
]

members.each do |member|
tasks << Proc.new do
Member.create! member.merge({:passord => "passord", :telefon => "22222222"})
tasks << proc do
Member.create! member.merge(passord: 'passord', telefon: '22222222')
end
end

members.each do |member|
tasks << Proc.new do
tasks << proc do
Member.find_by(mail: member[:mail]).roles << lim_web_role
end
end

print "00%"
print '00%'

tasks.each_with_index do |task, index|
task.call
print "\b\b\b%02d%%" % (100 * (index.to_f + 1) / tasks.length.to_f)
print format("\b\b\b%02d%%", (100 * (index.to_f + 1) / tasks.length.to_f))
end

print "\n"
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/samfundet_auth_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# desc "Explaining what the task does"
# task :samfundet_auth do
# # Task goes here
Expand Down
24 changes: 13 additions & 11 deletions samfundet_auth.gemspec
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
$:.push File.expand_path("../lib", __FILE__)
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)

# Maintain your gem's version:
require "samfundet_auth/version"
require 'samfundet_auth/version'

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "samfundet_auth"
s.name = 'samfundet_auth'
s.version = SamfundetAuth::VERSION
s.authors = ["MG::Web", "Jonas Amundsen"]
s.email = ["[email protected]", "[email protected]"]
s.homepage = "https://github.com/Samfundet/SamfundetAuth"
s.summary = "Authentication helper for applications of The student society of Trondheim."
s.description = "A mountable Rails engine which provides an application with the basic methods for authentication against the member database of The student society of Trondheim."
s.authors = ['MG::Web', 'Jonas Amundsen']
s.email = ['[email protected]', '[email protected]']
s.homepage = 'https://github.com/Samfundet/SamfundetAuth'
s.summary = 'Authentication helper for applications of The student society of Trondheim.'
s.description = 'A mountable Rails engine which provides an application with the basic methods for authentication against the member database of The student society of Trondheim.'

s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
s.test_files = Dir["test/**/*"]
s.files = Dir['{app,config,db,lib}/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.md']
s.test_files = Dir['test/**/*']

s.add_dependency "rails", "~> 5.2.0"
s.add_dependency 'rails', '~> 5.2.0'
s.required_ruby_version = '~> 2.5.5'
end
6 changes: 4 additions & 2 deletions script/rails
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/samfundet_auth/engine', __FILE__)
ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/samfundet_auth/engine', __dir__)

require 'rails/all'
require 'rails/engine/commands'