Main Tables Views Materialized Views Indexes Constraints Triggers Procedures Functions Packages Sequences Java Sources 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: 	version varchar2(100) := '';
     4: 
     5:     CURSOR server_group_by_label(org_id_in NUMBER, group_label_in VARCHAR2) IS
     6:     	   SELECT SG.*
     7: 	     FROM rhnServerGroupType SGT,
     8: 	     	  rhnServerGroup SG
     9: 	    WHERE SG.group_type = SGT.id
    10: 	      AND SGT.label = group_label_in
    11: 	      AND SG.org_id = org_id_in;
    12: 
    13:     FUNCTION find_server_group_by_type(org_id_in NUMBER,
    14:                                        group_label_in VARCHAR2)
    15:     RETURN NUMBER;
    16: 
    17:     procedure delete_org(org_id_in in number);
    18:     procedure delete_user(user_id_in in number, deleting_org in number := 0);
    19: 
    20: END rhn_org;

Package body source

Legend: comment string keyword reserved word operator
     1: PACKAGE BODY rhn_org
     2: IS
     3: 	body_version varchar2(100) := '';
     4: 
     5:     FUNCTION find_server_group_by_type(org_id_in NUMBER, group_label_in VARCHAR2)
     6:     RETURN NUMBER
     7:     IS
     8: 	server_group       server_group_by_label%ROWTYPE;
     9:     BEGIN
    10:     	OPEN server_group_by_label(org_id_in, group_label_in);
    11: 	FETCH server_group_by_label INTO server_group;
    12: 	CLOSE server_group_by_label;
    13: 
    14: 	RETURN server_group.id;
    15:     END find_server_group_by_type;
    16: 
    17:     procedure delete_org (
    18:         org_id_in in number
    19:     )
    20:     is
    21:         cursor users is
    22:         select id
    23:         from web_contact
    24:         where org_id = org_id_in;
    25: 
    26: 	cursor servers(org_id_in in number) is
    27:         select	id
    28:         from	rhnServer
    29:         where	org_id = org_id_in;
    30: 
    31:         cursor config_channels is
    32:         select id
    33:         from rhnConfigChannel
    34:         where org_id = org_id_in;
    35: 
    36: 	cursor custom_channels is
    37:         select	id
    38:         from	rhnChannel
    39:         where	org_id = org_id_in;
    40: 
    41: 	cursor errata is
    42:         select	id
    43:         from	rhnErrata
    44:         where	org_id = org_id_in;
    45: 
    46:     begin
    47: 
    48:         if org_id_in = 1 then
    49:             rhn_exception.raise_exception('cannot_delete_base_org');
    50:         end if;
    51: 
    52:         -- Delete all users.
    53:         for u in users loop
    54:             rhn_org.delete_user(u.id, 1);
    55:         end loop;
    56: 
    57:         -- Delete all servers.
    58:         for s in servers(org_id_in) loop
    59:             delete_server(s.id);
    60:         end loop;
    61: 
    62:         -- Delete all config channels.
    63:         for c in config_channels loop
    64:             rhn_config.delete_channel(c.id);
    65:         end loop;
    66: 
    67:         -- Delete all custom channels.
    68:         for cc in custom_channels loop
    69:           delete from rhnServerChannel where channel_id = cc.id;
    70:           delete from rhnServerProfilePackage where server_profile_id in (
    71:             select id from rhnServerProfile where base_channel = cc.id
    72:           );
    73:           delete from rhnServerProfile where base_channel = cc.id;
    74:         end loop;
    75: 
    76:         -- Delete all errata packages
    77:         for e in errata loop
    78:             delete from rhnErrataPackage where errata_id = e.id;
    79:         end loop;
    80: 
    81:         -- Give the org's entitlements back to the main org.
    82:         rhn_entitlements.remove_org_entitlements(org_id_in);
    83: 
    84:         -- Clean up tables where we don't have a cascading delete.
    85:         delete from rhnChannel where org_id = org_id_in;
    86:         delete from rhnDailySummaryQueue where org_id = org_id_in;
    87:         delete from rhnOrgQuota where org_id = org_id_in;
    88:         delete from rhnOrgInfo where org_id = org_id_in;
    89:         delete from rhnFileList where org_id = org_id_in;
    90:         delete from rhnServerGroup where org_id = org_id_in;
    91:         delete from rhn_check_suites where customer_id = org_id_in;
    92:         delete from rhn_command_target where customer_id = org_id_in;
    93:         delete from rhn_contact_groups where customer_id = org_id_in;
    94:         delete from rhn_notification_formats where customer_id = org_id_in;
    95:         delete from rhn_probe where customer_id = org_id_in;
    96:         delete from rhn_redirects where customer_id = org_id_in;
    97:         delete from rhn_sat_cluster where customer_id = org_id_in;
    98:         delete from rhn_schedules where customer_id = org_id_in;
    99: 
   100:         -- Delete the org.
   101:         delete from web_customer where id = org_id_in;
   102: 
   103:     end delete_org;
   104: 
   105: 	procedure delete_user(user_id_in in number, deleting_org in number := 0) is
   106: 		cursor is_admin is
   107: 			select	1
   108: 			from	rhnUserGroupType	ugt,
   109: 					rhnUserGroup		ug,
   110: 					rhnUserGroupMembers	ugm
   111: 			where	ugm.user_id = user_id_in
   112: 				and ugm.user_group_id = ug.id
   113: 				and ug.group_type = ugt.id
   114: 				and ugt.label = 'org_admin';
   115: 		cursor servergroups_needing_admins is
   116: 			select	usgp.server_group_id	server_group_id
   117: 			from	rhnUserServerGroupPerms	usgp
   118: 			where	1=1
   119: 				and usgp.user_id = user_id_in
   120: 				and not exists (
   121: 					select	1
   122: 					from	rhnUserServerGroupPerms	sq_usgp
   123: 					where	1=1
   124: 						and sq_usgp.server_group_id = usgp.server_group_id
   125: 						and	sq_usgp.user_id != user_id_in
   126: 				);
   127: 		cursor messages is
   128: 			select	message_id id
   129: 			from	rhnUserMessage
   130: 			where	user_id = user_id_in;
   131: 		users			number;
   132: 		our_org_id		number;
   133: 		other_users		number;
   134: 		other_org_admin	number;
   135:         other_user_id  number;
   136: 	begin
   137: 		select	wc.org_id
   138: 		into	our_org_id
   139: 		from	web_contact wc
   140: 		where	id = user_id_in;
   141: 
   142: 		-- find any other users
   143: 		begin
   144: 			select	id, 1
   145: 			into	other_user_id, other_users
   146: 			from	web_contact
   147: 			where	1=1
   148: 				and org_id = our_org_id
   149: 				and id != user_id_in
   150: 				and rownum = 1;
   151: 		exception
   152: 			when no_data_found then
   153: 				other_users := 0;
   154: 		end;
   155: 
   156: 		-- now do org admin stuff
   157: 		if other_users != 0 then
   158: 			for ignore in is_admin loop
   159: 				begin
   160: 					select	new_ugm.user_id
   161: 					into	other_org_admin
   162: 					from	rhnUserGroupMembers	new_ugm,
   163: 							rhnUserGroupType	ugt,
   164: 							rhnUserGroup		ug,
   165: 							rhnUserGroupMembers	ugm
   166: 					where	ugm.user_id = user_id_in
   167: 						and ugm.user_group_id = ug.id
   168: 						and ug.group_type = ugt.id
   169: 						and ugt.label = 'org_admin'
   170: 						and ug.id = new_ugm.user_group_id
   171: 						and new_ugm.user_id != user_id_in
   172: 						and rownum = 1;
   173: 				exception
   174: 					when no_data_found then
   175:                         -- If we're deleting the org, we don't want to raise
   176:                         -- the exception.
   177:                         if deleting_org = 0 then
   178:     						rhn_exception.raise_exception('cannot_delete_user');
   179:                         end if;
   180: 				end;
   181: 
   182: 				for sg in servergroups_needing_admins loop
   183: 					rhn_user.add_servergroup_perm(other_org_admin,
   184: 						sg.server_group_id);
   185: 				end loop;
   186: 			end loop;
   187: 		end if;
   188: 
   189: 		-- and now things for every user
   190: 		for message in messages loop
   191: 			delete
   192: 				from	rhnUserMessage
   193: 				where	user_id = user_id_in
   194: 					and message_id = message.id;
   195: 			begin
   196: 				select	1
   197: 				into	users
   198: 				from	rhnUserMessage
   199: 				where	message_id = message.id
   200: 					and rownum = 1;
   201: 				delete
   202: 					from	rhnMessage
   203: 					where	id = message.id;
   204: 			exception
   205: 				when no_data_found then
   206: 					null;
   207: 			end;
   208: 		end loop;
   209: 		delete from rhn_command_queue_sessions where contact_id = user_id_in;
   210: 		delete from rhn_contact_groups
   211: 		where recid in (
   212: 			select contact_group_id
   213: 			from rhn_contact_group_members
   214: 			where member_contact_method_id in (
   215: 				select recid from rhn_contact_methods
   216: 				where contact_id = user_id_in
   217: 				)
   218: 			)
   219: 			and not exists (
   220: 				select 1
   221: 				from rhn_contact_group_members, rhn_contact_methods
   222: 				where rhn_contact_groups.recid = rhn_contact_group_members.contact_group_id
   223: 					and rhn_contact_group_members.member_contact_method_id = rhn_contact_methods.recid
   224: 					and rhn_contact_methods.contact_id <> user_id_in
   225: 			);
   226: 		delete from rhn_contact_methods where contact_id = user_id_in;
   227: 		delete from rhn_redirects where contact_id = user_id_in;
   228: 		delete from rhnUserServerPerms where user_id = user_id_in;
   229:                 delete from rhnAppInstallSession where user_id = user_id_in;
   230: 		if other_users != 0 then
   231: 			update		rhnRegToken
   232: 				set		user_id = nvl(other_org_admin, other_user_id)
   233: 				where	org_id = our_org_id
   234: 					and user_id = user_id_in;
   235: 			begin
   236: 				delete from web_contact where id = user_id_in;
   237: 			exception
   238: 				when others then
   239: 					rhn_exception.raise_exception('cannot_delete_user');
   240: 			end;
   241:         -- Just Delete the user
   242: 		else
   243:             begin
   244:                 delete from web_contact where id = user_id_in;
   245: 		    exception
   246: 				when others then
   247: 					rhn_exception.raise_exception('cannot_delete_user');
   248: 			end;
   249: 		end if;
   250: 		return;
   251: 	end delete_user;
   252: 
   253: END rhn_org;