Main Tables Views Materialized Views Indexes Constraints Triggers Procedures Functions Packages Sequences Java Sources Jobs Sanity Check Index DDL scrips
Package source Package body source

RHN_ORG

DDL script

Package source

Legend: comment string keyword reserved word operator
     1: PACKAGE rhn_org
     2: IS
     3:     procedure delete_org(org_id_in in number);
     4:     procedure delete_user(user_id_in in number, deleting_org in number := 0);
     5: 
     6: END rhn_org;

Package body source

Legend: comment string keyword reserved word operator
     1: PACKAGE BODY rhn_org
     2: IS
     3:     procedure delete_org (
     4:         org_id_in in number
     5:     )
     6:     is
     7:         cursor users is
     8:         select id
     9:         from web_contact
    10:         where org_id = org_id_in;
    11: 
    12:     cursor servers(org_id_in in number) is
    13:         select    id
    14:         from    rhnServer
    15:         where    org_id = org_id_in;
    16: 
    17:         cursor config_channels is
    18:         select id
    19:         from rhnConfigChannel
    20:         where org_id = org_id_in;
    21: 
    22:     cursor custom_channels is
    23:         select    id
    24:         from    rhnChannel
    25:         where    org_id = org_id_in;
    26: 
    27:     cursor errata is
    28:         select    id
    29:         from    rhnErrata
    30:         where    org_id = org_id_in;
    31: 
    32:     begin
    33: 
    34:         if org_id_in = 1 then
    35:             rhn_exception.raise_exception('cannot_delete_base_org');
    36:         end if;
    37: 
    38:         -- Delete all users.
    39:         for u in users loop
    40:             rhn_org.delete_user(u.id, 1);
    41:         end loop;
    42: 
    43:         -- Delete all servers.
    44:         for s in servers(org_id_in) loop
    45:             delete_server(s.id);
    46:         end loop;
    47: 
    48:         -- Delete all config channels.
    49:         for c in config_channels loop
    50:             rhn_config.delete_channel(c.id);
    51:         end loop;
    52: 
    53:         -- Delete all custom channels.
    54:         for cc in custom_channels loop
    55:           delete from rhnServerChannel where channel_id = cc.id;
    56:           delete from rhnServerProfilePackage where server_profile_id in (
    57:             select id from rhnServerProfile where base_channel = cc.id
    58:           );
    59:           delete from rhnServerProfile where base_channel = cc.id;
    60:         end loop;
    61: 
    62:         -- Delete all errata packages
    63:         for e in errata loop
    64:             delete from rhnErrataPackage where errata_id = e.id;
    65:         end loop;
    66: 
    67:         -- Clean up tables where we don't have a cascading delete.
    68:         delete from rhnChannel where org_id = org_id_in;
    69:         delete from rhnDailySummaryQueue where org_id = org_id_in;
    70:         delete from rhnFileList where org_id = org_id_in;
    71:         delete from rhnServerGroup where org_id = org_id_in;
    72:         delete from rhnContentSource where org_id = org_id_in;
    73: 
    74:         -- Delete the org.
    75:         delete from web_customer where id = org_id_in;
    76: 
    77:     end delete_org;
    78: 
    79:     procedure delete_user(user_id_in in number, deleting_org in number := 0) is
    80:         cursor servergroups_needing_admins is
    81:             select    usgp.server_group_id    server_group_id
    82:             from    rhnUserServerGroupPerms    usgp
    83:             where    1=1
    84:                 and usgp.user_id = user_id_in
    85:                 and not exists (
    86:                     select    1
    87:                     from    rhnUserServerGroupPerms    sq_usgp
    88:                     where    1=1
    89:                         and sq_usgp.server_group_id = usgp.server_group_id
    90:                         and    sq_usgp.user_id != user_id_in
    91:                 );
    92:         users            number;
    93:         our_org_id        number;
    94:         other_users        number;
    95:         other_org_admin    number;
    96:         other_user_id  number;
    97:         is_admin       number;
    98:     begin
    99:         select    wc.org_id
   100:         into    our_org_id
   101:         from    web_contact wc
   102:         where    id = user_id_in;
   103: 
   104:         -- find any other users
   105:         begin
   106:             select    id, 1
   107:             into    other_user_id, other_users
   108:             from    web_contact
   109:             where    1=1
   110:                 and org_id = our_org_id
   111:                 and id != user_id_in
   112:                 and rownum = 1;
   113:         exception
   114:             when no_data_found then
   115:                 other_users := 0;
   116:         end;
   117: 
   118:         -- now do org admin stuff
   119:         if other_users != 0 then
   120:             -- is user admin?
   121:             select  count(1)
   122:              into   is_admin
   123:             from    rhnUserGroupType    ugt,
   124:                     rhnUserGroup        ug,
   125:                     rhnUserGroupMembers    ugm
   126:             where    ugm.user_id = user_id_in
   127:                 and ugm.user_group_id = ug.id
   128:                 and ug.group_type = ugt.id
   129:                 and ugt.label = 'org_admin';
   130:             if is_admin > 0 then
   131:                 begin
   132:                     select    new_ugm.user_id
   133:                     into    other_org_admin
   134:                     from    rhnUserGroupMembers    new_ugm,
   135:                             rhnUserGroupType    ugt,
   136:                             rhnUserGroup        ug,
   137:                             rhnUserGroupMembers    ugm
   138:                     where    ugm.user_id = user_id_in
   139:                         and ugm.user_group_id = ug.id
   140:                         and ug.group_type = ugt.id
   141:                         and ugt.label = 'org_admin'
   142:                         and ug.id = new_ugm.user_group_id
   143:                         and new_ugm.user_id != user_id_in
   144:                         and rownum = 1;
   145:                 exception
   146:                     when no_data_found then
   147:                         -- If we're deleting the org, we don't want to raise
   148:                         -- the exception.
   149:                         if deleting_org = 0 then
   150:                             rhn_exception.raise_exception('cannot_delete_user');
   151:                         end if;
   152:                 end;
   153: 
   154:                 for sg in servergroups_needing_admins loop
   155:                     rhn_user.add_servergroup_perm(other_org_admin,
   156:                         sg.server_group_id);
   157:                 end loop;
   158:             end if;
   159:         end if;
   160: 
   161:         -- and now things for every user
   162:         delete from rhnUserServerPerms where user_id = user_id_in;
   163:         update rhnConfigRevision
   164:            set changed_by_id = NULL
   165:          where changed_by_id = user_id_in;
   166:         if other_users != 0 then
   167:             update        rhnRegToken
   168:                 set        user_id = nvl(other_org_admin, other_user_id)
   169:                 where    org_id = our_org_id
   170:                     and user_id = user_id_in;
   171:         end if;
   172: 
   173:         begin
   174:             delete from web_contact where id = user_id_in;
   175:         exception
   176:             when others then
   177:                 rhn_exception.raise_exception('cannot_delete_user');
   178:         end;
   179:         return;
   180:     end delete_user;
   181: 
   182: END rhn_org;