Skip to content

Commit

Permalink
implementing block inactive users
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderUngefug committed Jun 7, 2024
1 parent 51b762c commit 001bf21
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 66 deletions.
3 changes: 3 additions & 0 deletions app/controllers/api/v1/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def session_params
def sign_in(user)
user.generate_session_token!(extended_session: session_params[:extend_session])

# Update the last_login attribute to the current time
user.update(last_login: Time.zone.now)

# Creates an extended_session cookie if extend_session is selected in sign in form.
if session_params[:extend_session]
cookies.encrypted[:_extended_session] = {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def current_user

# Returns whether hcaptcha is enabled by checking if ENV variables are set
def hcaptcha_enabled?
(ENV['HCAPTCHA_SITE_KEY'].present? && ENV['HCAPTCHA_SECRET_KEY'].present?)
ENV['HCAPTCHA_SITE_KEY'].present? && ENV['HCAPTCHA_SECRET_KEY'].present?
end

# Returns the current provider value
Expand Down
26 changes: 26 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ def new_user_signup_email
mail(to: emails, subject: t('email.new_user_signup.new_user'))
end

def inform_admins_blocked_users_inactivity_email
@user = params[:user]
Rails.logger.debug { "[UserMailer] Blocked user: #{@user.email}" }
emails = get_all_admin_emails
Rails.logger.debug { "[UserMailer] Admin emaild #{emails}" }
email = mail(to: emails, subject: t('email.blocked.account_blocked'))
if email.present?
Rails.logger.debug '[UserMailer] Email has been queued for delivery.'
else
Rails.logger.debug '[UserMailer] Failed to queue email for delivery.'
end
end

private

def preset
Expand All @@ -82,4 +95,17 @@ def admin_emails

User.where(role_id: role_ids).pluck(:email)
end

def get_all_admin_emails
# Find the role that corresponds to 'Administrator'
admin_role = Role.find_by(name: 'Administrator')

# Get all users with the 'Administrator' role
admins = User.where(role: admin_role)

# Return their email addresses
admin_emails = admins.pluck(:email)

admin_emails
end
end
6 changes: 3 additions & 3 deletions app/models/recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.search(input)
input: "%#{input}%").includes(:formats)
end

all.includes(:formats)
includes(:formats)
end

def self.public_search(input)
Expand All @@ -55,7 +55,7 @@ def self.public_search(input)
input: "%#{input}%").includes(:formats)
end

all.includes(:formats)
includes(:formats)
end

def self.server_search(input)
Expand All @@ -68,7 +68,7 @@ def self.server_search(input)
.includes(:formats)
end

all.includes(:formats)
includes(:formats)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.search(input)
def create_role_permissions
return if %w[Administrator User Guest SuperAdmin].include? name # skip creation for default roles

Permission.all.find_each do |permission|
Permission.find_each do |permission|
value = case permission.name
when 'CreateRoom', 'SharedList', 'CanRecord'
'true'
Expand Down
24 changes: 11 additions & 13 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def get_setting(name:)
.find_by(meeting_option: { name: })
end

# Autocreate all meeting options using the default
# Autocreate all meeting options using the default
def create_meeting_options
configs = RoomsConfiguration.joins(:meeting_option).where(provider: user.provider).pluck(:name, :value).to_h

MeetingOption.all.find_each do |option|
MeetingOption.find_each do |option|
value = if %w[true default_enabled].include? configs[option.name]
option.true_value
else
Expand All @@ -81,13 +81,11 @@ def public_recordings
end

def notify_room_deletion
begin
user_email = user.email
room_name = name
UserMailer.with(to: user_email, subject: "Your Room #{room_name} has been deleted", room_name: self.name).room_deletion_info.deliver_now
rescue => e
puts "Failed to send deletion email: #{e.message}"
end
user_email = user.email
room_name = name
UserMailer.with(to: user_email, subject: "Your Room #{room_name} has been deleted", room_name: name).room_deletion_info.deliver_now
rescue StandardError => e
Rails.logger.debug { "Failed to send deletion email: #{e.message}" }
end

private
Expand All @@ -113,10 +111,10 @@ def set_meeting_id

# Create unique pin for voice brige max 10^5 - 10000 unique ids
def set_voice_brige
if Rails.application.config.voice_bridge_phone_number != nil
id = SecureRandom.random_number((10.pow(5)) - 1)
raise if Room.exists?(voice_bridge: id) || id < 10000
unless Rails.application.config.voice_bridge_phone_number.nil?
id = SecureRandom.random_number(10.pow(5) - 1)
raise if Room.exists?(voice_bridge: id) || id < 10_000

self.voice_bridge = id
end
rescue StandardError
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def self.verify_activation_token(token)
user
end

def notify_admins_blocked_users_inactivity
UserMailer.with(user: self).inform_admins_blocked_users_inactivity_email.deliver_now
end

# Checkes the expiration of a token.
def self.activation_token_expired?(sent_at)
Time.current > (sent_at.in(ACTIVATION_TOKEN_VALIDITY_PERIOD))
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/current_room_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class CurrentRoomSerializer < ApplicationSerializer
include Presentable

attributes :id, :name, :presentation_name, :thumbnail, :online, :participants, :shared, :owner_name, :deletion_date
attributes :id, :name, :presentation_name, :thumbnail, :online, :participants, :shared, :owner_name, :deletion_date

attribute :last_session, if: -> { object.last_session }

Expand Down
3 changes: 2 additions & 1 deletion app/services/meeting_starter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def initialize(room:, base_url:, current_user:, provider:)

def call
# TODO: amir - Check the legitimately of the action.
options = RoomSettingsGetter.new(room_id: @room.id, provider: @room.user.provider, current_user: @current_user, only_bbb_options: true, voice_bridge: @room.voice_bridge).call
options = RoomSettingsGetter.new(room_id: @room.id, provider: @room.user.provider, current_user: @current_user, only_bbb_options: true,
voice_bridge: @room.voice_bridge).call
viewer_code = RoomSettingsGetter.new(
room_id: @room.id,
provider: @room.user.provider,
Expand Down
6 changes: 3 additions & 3 deletions app/services/room_settings_getter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def infer_can_record(room_settings:)
end

def set_voice_brige(room_settings:)
if @voice_bridge != nil
room_settings['voiceBridge'] = "#{@voice_bridge}"
end
return if @voice_bridge.nil?

room_settings['voiceBridge'] = @voice_bridge.to_s
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div style="padding-left: 80px; padding-right: 80px;">
<p style="font-size: 40px; margin-bottom: 20px; font-weight: 600;"><%= t('email.blocked.account_blocked') %></p>
<p style="font-size: 24px;"><%= t('email.blocked.account_blocked_description') %></p>
<p style="font-size: 20px;"><%= t('email.new_user_signup.name', name: @user.name) %></p>
<p style="font-size: 20px;"><%= t('email.new_user_signup.email', email: @user.email) %></p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<%= t('email.blocked.account_blocked') %>
<%= t('email.blocked.account_blocked_description') %>
<%= t('email.new_user_signup.name', name: @user.name) %>
<%= t('email.new_user_signup.email', email: @user.email) %>

1 change: 1 addition & 0 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# with Greenlight; if not, see <http://www.gnu.org/licenses/>.

# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Define an application-wide content security policy
Expand Down
1 change: 1 addition & 0 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# with Greenlight; if not, see <http://www.gnu.org/licenses/>.

# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format. Inflections
Expand Down
1 change: 1 addition & 0 deletions config/initializers/permissions_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# with Greenlight; if not, see <http://www.gnu.org/licenses/>.

# frozen_string_literal: true

# Define an application-wide HTTP permissions policy. For further
# information see https://developers.google.com/web/updates/2018/06/feature-policy
#
Expand Down
12 changes: 11 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,22 @@ de:
get_started: "Um dich zu registrieren, klicken bitte auf diese Schaltfläche."
valid_invitation: Die Einladung ist 24 Stunden gültig.
sign_up: Registrieren
new_user_signup:
new_user: Neue BigBlueButton Benutzeranmeldung
new_user_description: Ein neuer Benutzer hat sich angemeldet, um BigBlueButton zu nutzen.
name: "Name: %{name}"
email: "E-Mail: %{email}"
admin_panel: "Administrator-Panel"
take_action: "Um den neuen Benutzer zu sehen oder die notwendigen Maßnahmen zu ergreifen, besuchen Sie das Administrator-Panel"
reset:
password_reset: Passwort zurücksetzen
password_reset_requested: "Eine Passwortrücksetzung wurde für %{email} beantragt."
password_reset_confirmation: "Um das Passwort zurückzusetzen, klicke bitte auf die Schaltfläche unten."
reset_password: Passwort zurücksetzen
link_expires: Der Link wird in 1 Stunde ungültig.
ignore_request: "Wenn du dein Passwort nicht ändern wolltest, dann ignoriere bitte diese E-Mail."
blocked:
account_blocked: Konto eines Inaktiven Nutzers gesperrt
account_blocked_description: Ein Konto wurde aufgrund von Inaktivität gesperrt.
room:
new_room_name: "%{username}'s Raum"
new_room_name: "%{username}'s Raum"
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@ en:
reset_password: Reset Password
link_expires: The link will expire in 1 hour.
ignore_request: If you did not make a request to change your password, please ignore this email.
blocked:
account_blocked: Account Blocked of an Inactive User
account_blocked_description: A account has been blocked due to inactivity.
room:
new_room_name: "%{username}'s Room"
5 changes: 4 additions & 1 deletion cronjobs/delete_expired_rooms.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/bin/sh
echo "[$(date)]" >>/var/log/cron.log 2>&1
echo "This is a script running every minute" >>/var/log/cron.log 2>&1
cd /usr/src/app || {
echo "Failed to change directory to /usr/src/app" >>/var/log/cron.log 2>&1
exit 1
}

./bin/rake rooms:delete_expired >>/var/log/cron.log 2>&1
echo "Finished rake task" >>/var/log/cron.log 2>&1
echo "Finished rake task delete rooms" >>/var/log/cron.log 2>&1
./bin/rake users:block_inactive >>/var/log/cron.log 2>&1
echo "Finished rake task block users" >>/var/log/cron.log 2>&1
24 changes: 8 additions & 16 deletions db/data/20230328124724_populate_voice_brige_for_existing_rooms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@

class PopulateVoiceBrigeForExistingRooms < ActiveRecord::Migration[7.0]
def up
if Rails.application.config.voice_bridge_phone_number == nil
return
end
return if Rails.application.config.voice_bridge_phone_number.nil?

if Room.all.length > 89999
raise "The db contains to many rooms to assign each one a unique voice_bridge"
end
raise 'The db contains to many rooms to assign each one a unique voice_bridge' if Room.all.length > 89_999

Room.where(voice_bridge: nil).each do |room|
id = SecureRandom.random_number((10.pow(5)) - 1)
Room.where(voice_bridge: nil).find_each do |room|
id = SecureRandom.random_number(10.pow(5) - 1)

if id < 10000
id = id + 10000
end
id += 10_000 if id < 10_000

while Room.exists?(voice_bridge: id)
id = id + 1
if id >= 99999
id = 10000
end
id += 1
id = 10_000 if id >= 99_999
end

room.update(voice_bridge: id)
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/data/20231030185844_lowercase_emails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class LowercaseEmails < ActiveRecord::Migration[7.1]
def up
User.all.find_each(batch_size: 250) do |user|
User.find_each(batch_size: 250) do |user|
downcase = user.email.downcase
next if user.email == downcase

Expand Down
2 changes: 1 addition & 1 deletion db/data/20231117151542_add_email_on_sign_up_permission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def up

values = [{ role: admin, permission: email_permission, value: 'true' }]

Role.where.not(name: 'Administrator').each do |role|
Role.where.not(name: 'Administrator').find_each do |role|
values.push({ role:, permission: email_permission, value: 'false' })
end

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

class AddVoiceBrigeToRomms < ActiveRecord::Migration[7.0]
def change
add_column :rooms, :voice_bridge, :integer, null: true, default: nil
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240530133548_add_deletion_date_to_rooms.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddDeletionDateToRooms < ActiveRecord::Migration[7.1]
def change
add_column :rooms, :deletion_date, :datetime
Expand Down
26 changes: 26 additions & 0 deletions lib/tasks/block_inactive_users.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

namespace :users do
desc 'Block inactive Users'
task block_inactive: :environment do
days = ENV['USER_BLOCK_INACTIVITY'].to_i || 30
puts "A User is considered inactive if they have not logged in for #{days} days."
inactive_users = User.where('users.status !=2 AND users.last_login < ?',
days.days.ago).or(User.where('users.status !=2 AND users.last_login IS NULL AND users.created_at < ?',
days.days.ago))
inactive_users = inactive_users.includes(:role).where.not(roles: { name: 'Administrator' })
if inactive_users.any?
size = inactive_users.size
inactive_users.each do |user|
user.update(status: 2)
user.notify_admins_blocked_users_inactivity
puts "Blocked user: #{user.email}"
rescue StandardError => e
puts "Failed to block #{user.email}: #{e.message}"
end
puts "Blocked #{size} inactive users."
else
puts 'No inactive users to block.'
end
end
end
Loading

0 comments on commit 001bf21

Please sign in to comment.