summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/user.lua
blob: b536600132c23bf402d99b168a9cdb8fce14b648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Gemeinschaft 5 module: user class
-- (c) AMOOMA GmbH 2012-2013
-- 

module(...,package.seeall)

User = {}

MAX_GROUP_MEMBERSHIPS = 256;

-- create user object
function User.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'user';
  self.log = arg.log;
  self.database = arg.database;
  self.record = arg.record;
  return object;
end

-- find user by id
function User.find_by_id(self, id)
  local sql_query = 'SELECT * FROM `users` WHERE `id`= ' .. tonumber(id) .. ' LIMIT 1';
  local user = nil;

  self.database:query(sql_query, function(account_entry)
    user = User:new(self);
    user.record = account_entry;
    user.id = tonumber(account_entry.id);
    user.uuid = account_entry.uuid;
  end);

  return user;
end

-- find user by uuid
function User.find_by_uuid(self, uuid)
  local sql_query = 'SELECT * FROM `users` WHERE `id`= "' .. uuid .. '" LIMIT 1';
  local user = nil;

  self.database:query(sql_query, function(account_entry)
    user = User:new(self);
    user.record = account_entry;
    user.id = tonumber(account_entry.id);
    user.uuid = account_entry.uuid;
  end);

  return user;
end


function User.list_groups(self, id)
  require 'common.str'
  id = id or self.id;
  local sql_query = 'SELECT `b`.`name` FROM `user_group_memberships` `a` \
    JOIN `user_groups` `b` ON `a`.`user_group_id` = `b`.`id` \
    WHERE `a`.`state` = "active" AND `a`.`user_id`= ' .. tonumber(id) .. ' ORDER BY `b`.`position` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
  
  local groups = {};

  self.database:query(sql_query, function(entry)
    table.insert(groups, common.str.downcase(entry.name));
  end);

  return groups;
end


function User.check_pin(self, pin_to_check)
  if not self.record then
    return nil
  end

  local str_to_hash = tostring(self.record.pin_salt) .. tostring(pin_to_check);

  local file = io.popen("echo -n " .. str_to_hash .. "|sha256sum");
  local pin_to_check_hash = file:read("*a");
  file:close();

  pin_to_check_hash = pin_to_check_hash:sub(1, 64);

  if pin_to_check_hash == self.record.pin_hash then
    return true;
  end

  return false;
end