drop view radcheck;

CREATE VIEW radcheck AS 
  SELECT id, lower(mac) as UserName, 'Cleartext-Password'::text as Attribute, lower(mac) as Value, ':='::text as Op 
    from device_whitelist 
   where company = '3' 
 UNION 
  select id, lower(mac) as UserName, 'Cleartext-Password'::text as Attribute, lower(mac) as Value, ':='::text as Op 
    from phone 
   where company = '3' and status != 'deleted' 
 UNION 
  SELECT id, replace(lower(mac), ':', '') as UserName, 'Cleartext-Password'::text as Attribute, 
         replace(lower(mac), ':', '') as Value, ':='::text as Op 
    from device_whitelist 
   where company = '3' 
 UNION
  select id, replace(lower(mac), ':', '') as UserName, 'Cleartext-Password'::text as Attribute, 
         replace(lower(mac), ':', '') as Value, ':='::text as Op 
    from phone 
   where company = '3' and status != 'deleted' 
 UNION
  SELECT id, login as UserName, 'Cleartext-Password'::text as Attribute, pin as Value, ':='::text as Op 
   from contact 
  where company = '3';

GRANT UPDATE, SELECT, INSERT, DELETE ON radcheck TO astium;

