class Person

One person only

Public Instance Methods

account_total_due() click to toggle source
# File Entities/Person.rb, line 741
def account_total_due
  if account_due
    dputs(3) { "account_due is #{account_due.total.inspect}" }
    (account_due.total.to_f * 1000.0).round.to_i
  else
    _account_total_due
  end
end
add_internet_credit(client, internet_credit) click to toggle source
# File Entities/Person.rb, line 750
def add_internet_credit(client, internet_credit)
  dputs(3) { "Adding #{internet_credit}CFA to #{client.internet_credit} for #{client.login_name}" }
  internet_credit_before = client.internet_credit
  if internet_credit.to_i < 0 and internet_credit.to_i.abs > client.internet_credit.to_i
    internet_credit = -client.internet_credit.to_i
  end
  if pay_service(internet_credit, "internet_credit pour -#{client.login_name}:#{internet_credit}-")
    client.data_set(:_internet_credit, (client.internet_credit.to_i + internet_credit.to_i).to_s)
    log_msg(:AddCash, "#{self.login_name} added #{internet_credit} for #{client.login_name}: " +
                        "#{internet_credit_before} + #{internet_credit} = #{client.internet_credit}")
    log_msg(:AddCash, "Total due: #{account_total_due}")
  end
end
add_local_email() click to toggle source
# File Entities/Person.rb, line 695
def add_local_email
  add_user_account
  dir = "/home/#{login_name}/Maildir"
  if !File.exists? dir
    Dir.mkdir dir
    %w( new cur tmp ).each { |d|
      Dir.mkdir File.join(dir, d)
    }
    FileUtils.chown login_name, login_name, Dir.glob("/home/#{login_name}/**/**")
  end
  squirrel_base = '/srv/http/squirrelmail/config/var/data'
  if Files.exists? squirrel_base
    squirrel_pref = "#{squirrel_base}/#{login_name}.pref"
    if !File.exists? squirrel_pref
      IO.write(squirrel_pref, "full_name=#{full_name}\nemail_address=#{email}")
      FileUtils.chown 'http', 'http', squirrel_pref
    end
  end
  update_local_passwd(password)
end
add_user_account() click to toggle source
# File Entities/Person.rb, line 716
def add_user_account
  if not @proxy.has_storage? :LDAP
    if Persons.admin_users
      if !File.exist? "/home/#{login_name}"
        log_msg :Person, "Adding user-account for #{login_name} with #{permissions.inspect}"
        System.run_str("if which adduser; then adduser --disabled-password --gecos '#{self.full_name}' #{self.login_name};
          else useradd -m #{self.login_name}; fi")
      end
    end
  end
end
can_view(v) click to toggle source
# File Entities/Person.rb, line 951
def can_view(v)
  #Permission.can_view( data_get(:permissions), v )
  Permission.can_view(permissions, v)
end
check_pass(pass) click to toggle source
# File Entities/Person.rb, line 776
def check_pass(pass)
  if @proxy.has_storage? :LDAP
    # We have to try to bind to the LDAP
    dputs(2) { 'Trying LDAP' }
    #return @proxy.storage[:LDAP].check_login( data_get(:login_name), pass )
    return @proxy.storage[:LDAP].check_login(login_name, pass)
  else
    #dputs( 0 ){ "is #{pass} equal to #{data_get( :password ) }" }
    dputs(2) { "is #{pass} equal to #{password}" }
    #return pass == data_get( :password )
    return pass == password
  end
end
courses() click to toggle source
# File Entities/Person.rb, line 1043
def courses
  Courses.search_all.select { |c|
    c[:students] and c[:students].index(login_name)
  }
end
delete() click to toggle source
Calls superclass method
# File Entities/Person.rb, line 976
def delete
  Courses.search_all_.each { |course|
    dputs(3) { "Checking course #{course.name}" }
    [:teacher, :assistant, :responsible, :center].each { |role|
      begin
        r = course.data_get("_#{role}")
      rescue Exception => e
        if e.message == 'WrongIndex'
          dputs(0) { "Error: Role :#{role} is not well defined - resetting to nil" }
          course.data_set("_#{role}", nil)
        end
      end
      dputs(3) { "Role #{role} is #{r.inspect}" }
      if r and r.login_name == login_name
        raise IsNecessary.new(course)
      end
    }
  }

  Courses.data.values.each { |d|
    if d[:students] and d[:students].index(login_name)
      d[:students] -= [login_name]
    end
  }
  Shares.search_all_.each { |s|
    s.acl.delete login_name
  }
  AccessGroups.search_all_.each { |ag|
    ag.members and ag.members.delete login_name
  }
  Grades.search_by_student(self).each { |g|
    g.delete
  }

  if @proxy.has_storage? :LDAP
    if !Kernel.system("ldapdeleteuser #{self.login_name}")
      dputs(0) { "Error: couldn't delete user #{self.inspect}" }
    end
  elsif Persons.admin_users
    System.run_bool("if which deluser; then deluser #{self.login_name}; else
        userdel #{self.login_name}; fi")
  end
  if ConfigBase.has_function?(:share) && Persons.admin_users
    System.run_bool("smbpasswd -x #{self.login_name}")
  end
  super

  @proxy.resps = []
end
family_name=(v) click to toggle source
# File Entities/Person.rb, line 908
def family_name=(v)
  self._family_name = v.to_s.capitalize_all
end
first_name=(v) click to toggle source
# File Entities/Person.rb, line 904
def first_name=(v)
  self._first_name = v.to_s.capitalize_all
end
full_login() click to toggle source
# File Entities/Person.rb, line 824
def full_login
  "#{full_name} (#{login_name})"
end
full_name() click to toggle source
# File Entities/Person.rb, line 816
def full_name
  ret = []
  first_name and ret.push first_name
  family_name and ret.push family_name
  ret.length == 0 and ret.push login_name
  ret.join(' ')
end
gender_i18n(lang) click to toggle source
# File Entities/Person.rb, line 1164
def gender_i18n(lang)
  g = (gender ? gender.first : '').to_sym
  case lang
    when /en/
      {male: 'Mister', female: 'Madam'}[g]
    when /fr/
      {male: 'Monsieur', female: 'Madame'}[g]
    else
      ''
  end
end
get_all_due(person, date = Date.today) click to toggle source
# File Entities/Person.rb, line 935
def get_all_due(person, date = Date.today)
  if person.account_due && account_cash
    value = 0
    person.account_due.movements.each { |m|
      if m.date <= date
        dputs(3) { "Moving #{m.inspect}" }
        value += m.get_value(person.account_due)
        m.move_from_to(person.account_due, person.account_due_paid)
      end
    }
    dputs(3) { "Value is #{value}" }
    Movements.create('Transfert au comptable', date,
                     value, account_cash, person.account_due_paid)
  end
end
get_cash(person, amount) click to toggle source
# File Entities/Person.rb, line 912
def get_cash(person, amount)
  dputs(3) { "Amount is #{amount.inspect} and #{person.inspect} will receive it" }
  amount = amount.to_i
  if amount < 0
    dputs(0) { "Error: Can't transfer a negative amount here" }
    return false
  end
  if not person.account_due
    dputs(0) { "Error: #{person.login_name}::#{person.full_name} has no account_due" }
    return false
  end
  if not account_cash
    dputs(0) { "Error: #{self.inspect} has no account_cash" }
    return false
  end
  dputs(3) { "Transferring #{amount} from #{account_cash.get_path} to " +
      "#{person.account_due.get_path}"
  }
  Movements.create('Transfert au comptable', Date.today,
                   amount / 1000.0, account_cash, person.account_due)
  return true
end
get_unique() click to toggle source
# File Entities/Person.rb, line 1026
def get_unique
  login_name
end
groups=(g) click to toggle source
# File Entities/Person.rb, line 661
def groups=(g)
  self._groups = g
  update_smb_passwd
end
has_all_rights_of(person) click to toggle source
# File Entities/Person.rb, line 960
def has_all_rights_of(person)
  dputs(4) { "#{person.permissions} - #{permissions}" }
  pv1 = Permission.views(permissions)
  Permission.views(person.permissions).each { |p|
    found = false
    pv1.each { |p1|
      if p =~ /^#{p1}$/
        found = true
        dputs(4) { "Found my #{p1} matches his #{p}" }
      end
    }
    not found and return false
  }
  return true
end
has_permission?(perm) click to toggle source
# File Entities/Person.rb, line 1030
def has_permission?(perm)
  #dputs_func
  dputs(4) { "Checking #{perm.inspect} in #{permissions.inspect}" }
  dputs(4) { "Which is #{Permission.views(permissions).inspect }" }
  return false unless permissions
  return true if permissions.index(perm.to_s)
  Permission.views(permissions).select { |p|
    dputs(4) { "Checking permission #{p.inspect} of #{perm.inspect}" }
    dputs(4) { "Result is #{perm.to_s =~ /^#{p.to_s}$/}" }
    perm.to_s =~ /^#{p.to_s}$/
  }.length > 0
end
has_role(r) click to toggle source
# File Entities/Person.rb, line 956
def has_role(r)
  Permission.has_role(permissions, r)
end
is_responsible?() click to toggle source
# File Entities/Person.rb, line 1140
def is_responsible?
  %w( director teacher assistant center_director ).each { |p|
    return true if has_permission? p
  }
  return false
end
is_staff?() click to toggle source
# File Entities/Person.rb, line 1147
def is_staff?
  (permissions.to_a - %w(student internet)).length > 0
end
lp_cmd=(v) click to toggle source
# File Entities/Person.rb, line 835
def lp_cmd=(v)
  @proxy.print_card_student.lp_cmd = v
  @proxy.print_card_responsible.lp_cmd = v
end
password=(pass) click to toggle source
# File Entities/Person.rb, line 790
def password=(pass)
  (@pre_init || @loading) and return

  p = pass.to_s.chomp
  if @proxy.has_storage? :LDAP
    dputs(2) { "Changing password for #{self.login_name}: #{pass}" }
    p = System.run_str("slappasswd -s #{pass}").chomp
    dputs(2) { "Hashed password for #{self.login_name} is: #{pass}" }
  end
  update_smb_passwd(pass)
  update_local_passwd(pass)
  if self._password != p
    log_msg :person, "Setting password (#{pass}) for #{self.login_name} to #{p}"
    self._password = p
  end
  if (permissions and permissions.index('center')) or
      (groups and groups.index('share')) or
      (not self.password_plain or self.password_plain == '' or
          self.password_plain == pass)
    self.password_plain = pass
  else
    dputs(2) { self.password_plain.inspect }
    self.password_plain = '****'
  end
end
pay_service(credit, msg, date = nil) click to toggle source
# File Entities/Person.rb, line 764
def pay_service(credit, msg, date = nil)
  if account_due
    date = date ? Date.parse(date) : Date.today

    Movements.create("#{msg}", date.strftime('%Y-%m-%d'),
                     credit.to_i / 1000.0, account_due, ConfigBase.account_services)
    self.account_total_due = (account_due.total.to_f * 1000.0).round.to_i
  else
    return false
  end
end
permissions=(p) click to toggle source
# File Entities/Person.rb, line 666
def permissions=(p)
  has_teacher = self._permissions and self._permissions.concat(p).index('teacher')
  dputs(3) { "#{self.login_name}: has_teacher is #{has_teacher} - permissions are #{p.inspect}" }
  old_permissions = self._permissions
  self._permissions = p.uniq
  if has_teacher
    if Permission.can_view(p.reject { |perm| perm.to_s == 'admin' },
                           'FlagResponsible')
      Persons.responsibles_add(self)
    else
      Persons.responsibles_del(self)
    end
  end
  if permissions.index 'email'
    add_local_email
  end
  if (old_permissions || self._permissions).index('email')
    Persons.update_fetchmailrc
  end
  update_accounts
end
print(card = :student) click to toggle source
replace(orig, field, str) click to toggle source
# File Entities/Person.rb, line 828
def replace(orig, field, str)
  fields.each { |f|
    orig.gsub!(f[0], f[1].to_s)
  }
  orig
end
report_list(report, date = nil) click to toggle source
# File Entities/Person.rb, line 1076
def report_list(report, date = nil)
  date ||= Date.today
  case report
    when :daily, 1
      report_list_movements(date)
    when :weekly, 2
      week = Date.commercial(date.year, date.cweek, 1)
      report_list_movements(week, week + 6)
    when :monthly, 3
      report_list_movements(
          Date.new(date.year, date.month, 1),
          Date.new(date.year, date.month, -1))
    when :all, 4
      report_list_movements(nil, date)
    when :all_paid, 5
      report_list_movements(nil, nil, account_due_paid)
  end
end
report_list_movements(from = nil, to = from, account = account_due) click to toggle source
# File Entities/Person.rb, line 1049
def report_list_movements(from = nil, to = from, account = account_due)
  total = 0
  account or return [[]]
  if from
    account.movements.select { |m|
      dputs(3) { "Date is #{m.date.inspect}" }
      (from..to).include? m.date
    }
  elsif to
    dputs(3) { "Fetching only dates upto #{to}" }
    account.movements.select { |m|
      dputs(3) { "Date is #{m.date.inspect}" }
      m.date <= to
    }
  else
    account.movements
  end.reverse.collect { |m|
    v = m.get_value(account)
    [m.global_id,
     [m.date,
      "#{m.get_other_account(account).name}: #{m.desc}",
      Account.total_form(v),
      Account.total_form(total += v)]
    ]
  }.reverse
end
report_pdf(report, date = nil) click to toggle source
# File Entities/Person.rb, line 1095
def report_pdf(report, date = nil)
  file = "/tmp/cash_#{login_name}.pdf"
  Prawn::Document.generate(file,
                           :page_size => 'A4',
                           :page_layout => :portrait,
                           :bottom_margin => 2.cm) do |pdf|

    sum = 0
    movs = report_list(report, date).reverse
    pdf.text "Report for #{full_name}", :align => :center, :size => 20
    pdf.font_size 10
    if movs.length > 0
      pdf.text "From #{movs.first[1][0]} to #{movs.last[1][0]}"
    end
    pdf.text "Account: #{account_due.path}"
    pdf.move_down 1.cm

    if movs.length > 0
      header = [['Date', 'Description', 'Value', 'Sum'].collect { |ch|
                  {:content => ch, :align => :center} }]
      dputs(3) { "Movs is #{movs.inspect}" }
      pdf.table(header + movs.collect { |m_id, m|
                  [{:content => "#{m[0]}", :align => :center},
                   m[1],
                   {:content => "#{m[2]}", :align => :right},
                   {:content => "#{Account.total_form(
                       sum += m[2].gsub(',', '').to_f / 1000)}",
                    :align => :right}]
                }, :header => true, :column_widths => [70, 300, 75, 75])
      pdf.move_down(2.cm)
    end

    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text "#{Date.today} - #{account_due.path}",
                    :at => [0, -20], :size => 10
      pdf.draw_text pdf.page_number, :at => [19.cm, -20]
    end
  end
  file
end
session() click to toggle source
# File Entities/Person.rb, line 900
def session
  Sessions.match_by_sid(self.session_id)
end
setup_instance() click to toggle source
# File Entities/Person.rb, line 600
def setup_instance
  dputs(3) { "Data is #{@proxy.data[@id].inspect}" }

  self.internet_credit = internet_credit.to_i
  update_accounts
end
show_password?(user = nil) click to toggle source
# File Entities/Person.rb, line 1151
def show_password?(user = nil)
  case ConfigBase.show_passwords.first
    when 'always'
      return true
    when 'never'
      return false
    when 'students'
      return !is_staff?
    when 'lesser'
      return user ? user.has_all_rights_of(self) : false
  end
end
simple() click to toggle source
# File Entities/Person.rb, line 885
def simple
  return true if !permissions
  (permissions-%w(internet student)).length == 0
end
to_frontend(owner) click to toggle source
# File Entities/Person.rb, line 1136
def to_frontend(owner)
  to_list_id(owner)
end
to_list(user = nil) click to toggle source
# File Entities/Person.rb, line 890
def to_list(user = nil)
  [login_name, "#{full_name} - #{login_name}" +
                 (show_password?(user) ? ":#{password_plain}" : '')]
end
to_list_id(user = nil) click to toggle source
# File Entities/Person.rb, line 895
def to_list_id(user = nil)
  [person_id, "#{full_name} - #{login_name}" +
                (show_password?(user) ? ":#{password_plain}" : '')]
end
total_cash() click to toggle source
# File Entities/Person.rb, line 653
def total_cash
  if account_cash
    (account_cash.total.to_f * 1000).to_i
  else
    0
  end
end
update_account_cash() click to toggle source
# File Entities/Person.rb, line 640
def update_account_cash
  return unless ConfigBase.has_function?(:accounting)
  if can_view(:FlagAccounting) && login_name != 'admin'
    acc = (first_name || login_name).capitalize
    dputs(3) { "Getting cash account #{acc}" }
    return unless ConfigBase.account_cash
    cc = "#{ConfigBase.account_cash.get_path}::#{acc}"
    dputs(3){"Account will be #{cc}"}
    self.account_cash = Accounts.get_by_path_or_create(cc, cc, false, -1, true)
    dputs(3) { "Account is #{account_cash.inspect}" }
  end
end
update_account_due() click to toggle source
# File Entities/Person.rb, line 613
def update_account_due
  # dputs_func
  return unless ConfigBase.has_function? :accounting
  if can_view :FlagAddInternet and login_name != 'admin'
    dputs(3) { "Adding account_due to -#{login_name.inspect}-" }
    if login_name.to_s == ''
      dputs(0) { "Error: account_name_due empty and no login_name #{self.inspect}" }
      return
    end
    acc = (full_name || login_name).capitalize_all
    return unless ConfigBase.account_lending
    lending = "#{ConfigBase.account_lending.get_path}::#{acc}"
    service = ConfigBase.account_services.get_path
    dputs(2) { "Preparing accounts for #{full_name} - #{acc}" }
    if !self.account_due
      dputs(2) { "Creating account #{lending}" }
      self.account_due = Accounts.get_by_path_or_create(lending,
                                                        acc, false, -1, true)
    end
    if !self.account_due_paid
      dputs(2) { "Creating account #{lending}::Paid" }
      self.account_due_paid = Accounts.get_by_path_or_create("#{lending}::Paid",
                                                             acc, false, -1, true)
    end
  end
end
update_accounts() click to toggle source
# File Entities/Person.rb, line 607
def update_accounts
  return unless ConfigBase.has_function? :accounting
  update_account_cash
  update_account_due
end
update_local_passwd(pass) click to toggle source
# File Entities/Person.rb, line 688
def update_local_passwd(pass)
  if permissions and permissions.index 'email'
    dputs(2) { "Updating password #{pass} for #{login_name}" }
    System.run_bool("echo -e '#{pass}\n#{pass}' | passwd #{login_name}")
  end
end
update_smb_passwd(pass = password_plain) click to toggle source
# File Entities/Person.rb, line 728
def update_smb_passwd(pass = password_plain)
  if ConfigBase.has_function?(:share) && (groups && groups.index('share'))
    add_user_account
    if Persons.admin_users && pass
      p = pass.chomp
      log_msg :person, "Changing password in Samba to #{p.inspect}"
      pwd_change = "/bin/echo -e '#{p}\\n#{p}' | smbpasswd -s -a #{self.login_name} "
      dputs(3) { pwd_change.inspect }
      System.run_str pwd_change
    end
  end
end