Check oracle table for an expiry date and send mail - oracle

I have a table pro where I have following fields
id, product name, email and expiry
I am trying to send an email from oracle when expiry date match with system date.
Is it possible through oracle jobs/scheduler or any other way?

You can use the query below to find a list of users. In my example, I have 120 days so modify it to fit your needs.
In addition, you will need a place to store the email address for each user than JOIN the tables and you can write some code to loop through the list and send mail.
Depending on your platform you can use utilities like mail or setup an Oracle mail server, which is a bit involved.
select username as “USER NAME”, expiry_date as “EXPIRE DATE”, account_status
from dba_users
where expiry_date < sysdate+120
and account_status IN ( ‘OPEN’, ‘EXPIRED(GRACE)’ )
order by account_status, expiry_date, username;

Related

Comparing a Table column to the connected user in Oracle

im in needed of help, im doing some exercises with a a oracle database and do not know how to do this:
I have a table called users that have information of the users that connect to the DB, what i want is a procedure where to show the information of a specific row where the connected user is compared to the user in the table.
i do not how to compared a data in a table to the connected user, more like i dont know how "work" or what limitations have the "user" parameter to be implemented this way
Sorry if my petition its a little confusing english is not my main language.
EX: i have a user in the table users, that have a serial id, username,password,name, surname1 and surname2 and i want the procedure to show me the information of this user in particular, but i do want that if for example im connected with "pedro" user to the db this procedure show me the info about the user pedro and if i change the user connected to another like Paul the information of the select change to Paul.
One option might be to use the USER function.
SQL> connect scott/tiger#orcl
Connected.
SQL> select user from dual;
USER
------------------------------
SCOTT
SQL>
Therefore you'd
select serial id,
username,
password,
name,
surname1,
surname2
from users
where username = user;
You can find the currently connected user with
select sys_context('USERENV','CURRENT_USER') from dual;

How to know user's "expiry_date" changed?

I'm faced with a very specific task with Oracle.
I need to prove that values in column expiry_date of table dba_users were not changed in a certain period (during last weekend).
I am not very experienced with Oracle. For me, it seems like a feature called "audit" is disabled, but I'm not strongly sure.
So, I need whatever option to check whether these values have been changed. I have already tried to analyze the user_sessions table (having the intent to check for connections during the period in question), but with that, I couldn't get an exact answer.
Instead of trying to directly detect changes to dba_users.expiry_date, it would be much easier to detect when a user changes their password, which would indirectly change the expiry date:
select password_date
from sys.user_history$
join dba_users
on sys.user_history$.user# = dba_users.user_id
where username = '&USER'
order by password_date desc;
However, that table will only contain values if the user has a profile where the PASSWORD_REUSE_TIME is not set to unlimited. You can find that setting with these queries:
--Find the user's profile:
select profile from dba_users where username = '&USER';
--Find the PASSWORD_REUSE_TIME of the profile.
--If the value is 'DEFAULT', you'll have to run this query again for the profile 'DEFAULT'.
select limit
from dba_profiles
where resource_name = 'PASSWORD_REUSE_TIME'
and profile = '&PROFILE';
(Oracle's audit functionality does not capture this information. You can run audit alter user and then query DBA_AUDIT_TRAIL to detect when a user was changed, but you can't tell the difference between a password reset and an account locking.)
Unfortunately, the above solution doesn't cover all possible reasons why the expiration date could change. If an administrator manually expires a user, or changes a profile or profile settings, the expiration date could change. To capture all possible reasons for a change, you would need to create triggers on the system tables used by DBA_USERS and store the expiration value whenever it changes. Or you could create a job that periodically checks the view and looks for changes.
But before you consider those options, you might want to explain exactly why you want this information. I'm guessing this question is for a security audit, but this feels like the wrong kind of information to look for. What is your true goal here?

Oracle find all users I created (excluding Oracle accounts)

Is there a way to find all user accounts in Oracle which have been created by me?
The SELECT * FROM ALL_USERS; returns all users in Oracle, but there seems to be no way of defining the 'owner' of the account (ORC_SYS would be nice) so I can add a clause like
'WHERE OWNER !+ 'ORC_SYS' or something.
Thanks in advance
KS
If its only the users created by oracle at install time you want to exclude you can properly do it filtering on user_id. Normally these users will have the lowest numbers:
Ajust the 35 to your installation.
Select * from dba_users where user_id > 35;
And if you are running 12c or above there is a column "Oracle_maintained" telling you if it is an oracle created user.
Select * from dba_users where oracle_maintained = 'N';
If you have access to dba_users and your Oracle's version is 12.1 and above, you can filter by oracle_maintained column.
Otherwise, there is no "official" way to distinguish between oracle internal users and your own.
There are some indirect ways, though:
Filter by all_users.created column. Mostly, internal users are created when the database is created, so your users will be after this date. You may find the database creation time in v$database.created .
Filter by all_users.user_id. As above, mostly, internal users are created when the database is created, so they get low user_id. There are some exceptions for several users like SYSBACKUP, SYSDG.
Both ways may produce incorrect results when internal users are created much later than the database creation - for example when an Oracle Option is reinstalled.
Should you try:
SELECT * FROM dba_users;

how to check last connected user,time in oracle?

please let me know
How to Find When the User Last Logged into the Database?
please let me know how to check this information with the command.
Enable auditing.
Then audit connects - very easy command
audit connect
Docs Link Here
Then do some connects.
Then query sys.dba_audit_session -
SELECT
username,
timestamp
FROM
sys.dba_audit_session
WHERE
username = 'HR' -- the user you care about
AND action_name = 'LOGON'
ORDER BY
timestamp DESC
FETCH FIRST 1 ROWS ONLY -- in 11g or older just also say where rownum < 2
Try this:
select username, machine, to_char(logon_time,'HH:MM:SS')
from v$session
where username='SYS' <-- username

View restriction not working

I need to create views depending on users on a oracle database
For that, using System I use the following querys:
CREATE OR REPLACE VIEW PROT_VIEW AS SELECT USER_ID, ORDER_DATE, ORDER_DESC
FROM PROT
WHERE USER_ID=USER;
the tables and the values of them are as follows:
CREATE TABLE PROT(
USER_ID VARCHAR2(10),
ORDER_DATE DATE,
ORDER_DESC VARCHAR2(60));
INSERT INTO PROT VALUES ('ADM',SYSDATE+4,'FOUR DAYS LATER');
INSERT INTO PROT VALUES ('ADM',SYSDATE+5,'FIVE DAYS LATER');
INSERT INTO PROT VALUES ('STUD1',SYSDATE+6,'SIX DAYS LATER');
INSERT INTO PROT VALUES ('STUD2',SYSDATE+7,'SEVEN DAYS LATER') ;
After this I have 3 different users (adm,stud1 and stud2) and when I log them on I should be getting 3 different results (one for each user) from each select I do (depending on the user logged).
The problem is, no matter what which user I have logged in (system,adm,stud1,stud2) I get empty tables.
I would like to know what i'm doing wrong and what can I do to solve this problem
Thank you in advance for anyone who's willing to help
update: i've been messing around and the problem is that i cant connect to those users. i've granted create session with system to those users and tried to connect to them but I'm stuck on system
Nice way to accomplish this task is to use public synonym in my opinion.
After creating your view (prot_view) on system schema, create a public synonym with the same name as view :
create or replace public synonym prot_view for prot_view;
and issue :
grant select on prot_view to public;
to be able to get desired result from every schema without prefixing with system schema name :
select * from prot_view;
"the problem is that i cant connect to those users"
user is a pseduo-column which returns the name of the account you're currently connected as. You're logged in as SYSTEM so that's the value of user, and that's why your view returns no rows. So, contrary to your question title, the view restriction is working.
Which means the the real question is, why can't you connect as those other users? You have SYSTEM so you have the necessary privileges to straighten out the accounts by changing the passwords to something you know or granting create session, or whatever.

Resources