I would like to log all user activity. (Who used when which controller and action)
The site is only for a small (max. 100 overall, max. 10 online) group of people.
I would extend CController with afterAction() => If an user id is set, I get the current controller with getId() and the current action with getAction(), then write this data with a timestamp(CTimestampBehavior) into the database. Is this an valid approach?
If you only want to record activity done via controllers and all your controllers (present and future) will extend CController then yes, you can do it as you suggested.
Be advised that I’m not sure if afterAction() is called if an exception will be thrown, and it will be, in normal operation of the site (averagely speaking - if your site throws 404s, 400 etc via exceptions). Consider overriding beforeAction() instead.
I believe another approach could be to override some method in Yii base class and thus you could achieve this logging without your custom controller method running. This can be useful if you use 3rd party extensions that will not use your custom controller. Sorry, but I don’t have candidates to throw at you at this moment.
I’ve done the same for an application recently. I created a class (AuditMessage) with a static function ‘log’ that I call whenever any action is executed successfully. Remember that one can access an action that updates a certain object but may retrieve an error message (e.g. when validation fails). You have to consider for yourself whether or not you would want to log such actions.