00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_RUN_AS_BLOCK_HPP 00008 #define COH_RUN_AS_BLOCK_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 #include "coherence/security/auth/Subject.hpp" 00013 00014 COH_OPEN_NAMESPACE2(coherence,security) 00015 00016 COH_OPEN_NAMESPACE(auth) 00017 class Subject; 00018 COH_CLOSE_NAMESPACE 00019 00020 using coherence::security::auth::Subject; 00021 00022 /** 00023 * The RunAsBlock class allows for easy creation of scoped authorization code 00024 * blocks based on a Subject. The RunAsBlock object will 00025 * push the Subject onto a thread-local "current subject" upon entering the 00026 * block, and pop the subject upon exiting the code block. 00027 * 00028 * A more friendly form is to use the COH_RUN_AS macro. Example usage: 00029 * 00030 * COH_RUN_AS(vSubject) 00031 * { 00032 * // code here will considered to be run by the supplied subject 00033 * // ... 00034 * // ... 00035 * } // subject will be reverted 00036 * 00037 * @author mf 2008.08.22 00038 */ 00039 class COH_EXPORT RunAsBlock 00040 { 00041 // ----- constructors --------------------------------------------------- 00042 00043 public: 00044 /** 00045 * Construct a RunAsBlock object. 00046 * 00047 * This will automatically set the thread's Subject. 00048 */ 00049 RunAsBlock(TypedHandle<const Subject> vSubject); 00050 00051 /** 00052 * Copy constructor, for use by COH_RUN_AS macro. 00053 * 00054 * The new block takes over the ownership of the pop. 00055 */ 00056 RunAsBlock(const RunAsBlock& that); 00057 00058 /** 00059 * Destroy a RunAsBlock object. 00060 * 00061 * This will automatically pop the Subject. 00062 */ 00063 ~RunAsBlock(); 00064 00065 00066 // ----- operators ------------------------------------------------------ 00067 00068 public: 00069 /* 00070 * Boolean conversion for use in COH_RUN_AS macro. 00071 * 00072 * @return false always 00073 */ 00074 operator bool() const; 00075 00076 private: 00077 /** 00078 * Blocked assignment operator. 00079 */ 00080 const RunAsBlock& operator=(const RunAsBlock&); 00081 00082 /** 00083 * Blocked dynamic allocation. 00084 */ 00085 static void* operator new(size_t); 00086 00087 00088 // ----- data members --------------------------------------------------- 00089 00090 private: 00091 /** 00092 * Subject associated with the block. 00093 */ 00094 mutable TypedHandle<const Subject> m_vSubject; // on stack 00095 }; 00096 00097 COH_CLOSE_NAMESPACE2 00098 00099 00100 /** 00101 * Macro for making more readable run-as code blocks See the documentation of 00102 * RunAsBlock for a usage example. 00103 * 00104 * @see coherence::security::RunAsBlock 00105 */ 00106 #define COH_RUN_AS(VSUBJECT) \ 00107 if (coherence::security::RunAsBlock COH_UNIQUE_IDENTIFIER(_coh_runas_) \ 00108 = coherence::security::RunAsBlock(VSUBJECT)) \ 00109 { \ 00110 COH_THROW(coherence::lang::IllegalStateException::create()); \ 00111 } \ 00112 else 00113 00114 #endif // COH_RUN_AS_BLOCK_HPP